<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[programming - ]]></title><description><![CDATA[programming - ]]></description><link>http://carltonmatthews.com/</link><generator>Ghost 0.5</generator><lastBuildDate>Wed, 22 Apr 2026 18:04:58 GMT</lastBuildDate><atom:link href="http://carltonmatthews.com/tag/programming/rss/" rel="self" type="application/rss+xml"/><ttl>60</ttl><item><title><![CDATA[Back in the Saddle Again - JavaScript Edition]]></title><description><![CDATA[<h2 id="enterjavascript">Enter JavaScript</h2>

<p>I am working through the javascript lessons over at <a href="http://freecodecamp.com/">Free Code Camp</a>.  Towards the end of last year they added a bunch of new lessons to their javascript course as well as a bunch of new topics.  If you are looking to hone/learn new development skills Free Code Camp is a great site.  <a href="http://www.freecodecamp.com/cbmatthews">Here is my page.</a></p>

<p>As much as I enjoy writing code my current job does not afford me the oppurtunity.  In fact it is outside the scope of my work as a Systems Engineer.  I also learned that while I have a pretty high opinion of my skills I got some feedback that there was a perception that I was on the slow side in terms of coding speed.</p>

<p>But I digress.  I was working on Profile Lookup assignment.  </p>

<p><strong><em>Given an array of objects you need to search for a record and retrieve a property from that record.  If the record or property doesn't exsist you need to respond appropriately.</em></strong></p>

<p>Here is my array of objects <br>
<code>var characters = [
    {
        "number": "0543236543",
        "firstName": "Clark",
        "lastName": "Kent",
        "catchPhrase": "Up Up And Away!",
        "weaknesses": ["Kryptonite", "Magic", "Lois Lane"]
    },
    {
        "number": "0543236544",
        "firstName": "Bruce",
        "lastName": "Wayne",
        "catchPhrase": "Because I'm Batman!",
        "weaknesses": ["Old Age", "Nothing"]
    },
    {
        "number": "0543236545",
        "firstName": "Cain",
        "lastName": "Marko",
        "catchPhrase": "I'm The Juggernaut Fair Lady!",
        "weaknesses": ["Telepathy", "Wolverine", "Hulk"]
    },
    {
        "number": "0543236546",
        "firstName": "Alexander",
        "lastName": "Luthor",
        "catchPhrase": "I'm The Greatest Criminal Mind of Our Time.",
        "weaknesses": ["Superman", "Ego", "Old Age"]
    },
];</code></p>

<h3 id="createyourfunction">Create your function</h3>

<p>We have to create a function that takes in our ID and the property that we want returned.  Here is the stub</p>

<p><code>function lookUp(number, prop){
    //Do Stuff
}</code></p>

<h3 id="searchfortherecord">Search for the record</h3>

<p>There are a couple of ways to attack this challenge.  You can use a <strong>FOR</strong> loop to cycle through each object in the array. <br>
<code>for (var x = 0; x &lt; characters.length; x++) {
    if (characters[x].number === number) {
        //Do Stuff
    }
 }</code></p>

<p>While this approach would work I wanted to try something different.  Enter the <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter">Array.Prototype.Filter.</a>  From the Mozilla Page:</p>

<blockquote>
  <p>The <strong>filter()</strong> method creates a new array with all elements that pass the test implemented by the provided function.</p>
</blockquote>

<p>The filter takes a callback as a parameter.  Here is how I implemented the number search using the filter.</p>

<p><code>function lookUp(number, prop){
  var character = characters.filter(function(obj) {
    return obj.number === number;
  });
}</code></p>

<p>This code looks through the entire array of objects and tests the number property to see if it exactly matches the number passed in as a parameter.  It then returns an array of objects.  In this example it will return and array with one object.</p>

<h3 id="lookingforerrors">Looking for errors</h3>

<p>After retrieving records we need to check if any records match our search parameter and if the property we want is valid.  Here is how we implement this check.</p>

<p><code>if (character != "") {
    result = character[0].hasOwnProperty(prop) ? character[0][prop] : "No such property";
} else {
    result = "No such contact";
}
  return result;
}</code></p>

<h3 id="puttingitalltogether">Putting it all together</h3>

<p><code>var characters = [
    {
        "number": "0543236543",
        "firstName": "Clark",
        "lastName": "Kent",
        "catchPhrase": "Up Up And Away!",
        "weaknesses": ["Kryptonite", "Magic", "Lois Lane"]
    },
    {
        "number": "0543236544",
        "firstName": "Bruce",
        "lastName": "Wayne",
        "catchPhrase": "Because I'm Batman!",
        "weaknesses": ["Old Age", "Nothing"]
    },
    {
        "number": "0543236545",
        "firstName": "Cain",
        "lastName": "Marko",
        "catchPhrase": "I'm The Juggernaut Fair Lady!",
        "weaknesses": ["Telepathy", "Wolverine", "Hulk"]
    },
    {
        "number": "0543236546",
        "firstName": "Alexander",
        "lastName": "Luthor",
        "catchPhrase": "I'm The Greatest Criminal Mind of Our Time.",
        "weaknesses": ["Superman", "Ego", "Old Age"]
    },
];</code></p>

<p><code>function lookUp(number, prop){
    var result;
    var character = characters.filter(function(obj) {
        return obj.number === number;
      });
      if (character != "") {
    result = character[0].hasOwnProperty(prop) ? character[0][prop] : "No such property";
    } else {
    result = "No such contact";
  }
  return result;
}</code></p>

<p>If you call <br>
<code>lookUp("0543236546", "weaknesses")</code></p>

<p>The output will be <br>
<code>["Kryptonite", "Magic", "Lois Lane"]</code></p>

<p>Fun times coding.  Do you have any suggestions on how I can make this code better?  Do you have another approach to doing this?  Leave a comment and let me know.</p>

<p>Carlton</p>]]></description><link>http://carltonmatthews.com/back-in-the-saddle-again-javascript-edition/</link><guid isPermaLink="false">5a6aa68f-7942-45ab-9093-9a003d8a851e</guid><category><![CDATA[show_your_work]]></category><category><![CDATA[javascript]]></category><category><![CDATA[programming]]></category><category><![CDATA[freecodecamp]]></category><dc:creator><![CDATA[Carlton Matthews]]></dc:creator><pubDate>Wed, 27 Jan 2016 23:44:32 GMT</pubDate></item></channel></rss>