Back in the Saddle Again - JavaScript Edition

Enter JavaScript

I am working through the javascript lessons over at Free Code Camp. 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. Here is my page.

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.

But I digress. I was working on Profile Lookup assignment.

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.

Here is my array of objects
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"] }, ];

Create your function

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

function lookUp(number, prop){ //Do Stuff }

Search for the record

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

While this approach would work I wanted to try something different. Enter the Array.Prototype.Filter. From the Mozilla Page:

The filter() method creates a new array with all elements that pass the test implemented by the provided function.

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

function lookUp(number, prop){ var character = characters.filter(function(obj) { return obj.number === number; }); }

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.

Looking for errors

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.

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

Putting it all together

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"] }, ];

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; }

If you call
lookUp("0543236546", "weaknesses")

The output will be
["Kryptonite", "Magic", "Lois Lane"]

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.

Carlton