Profile Lookup endeavour

Profile Lookup endeavour
0

#1

Hi. I’m stuck on Profile Lookup.

Here’s my code:

//Setup
var contacts = [
    {
        "firstName": "Akira",
        "lastName": "Laine",
        "number": "0543236543",
        "likes": ["Pizza", "Coding", "Brownie Points"]
    },
    {
        "firstName": "Harry",
        "lastName": "Potter",
        "number": "0994372684",
        "likes": ["Hogwarts", "Magic", "Hagrid"]
    },
    {
        "firstName": "Sherlock",
        "lastName": "Holmes",
        "number": "0487345643",
        "likes": ["Intriguing Cases", "Violin"]
    },
    {
        "firstName": "Kristian",
        "lastName": "Vos",
        "number": "unknown",
        "likes": ["JavaScript", "Gaming", "Foxes"]
    }
];


function lookUpProfile(name, prop)
{
    // Only change code below this line
    for (var i = 0; i < contacts.length; i++) 
    {
        if (firstName == contacts[i].firstName) {
            if (contacts[i].hasOwnProperty(prop)) {
                return contacts[i][prop];
            } 
            else 
            {
                return “No such property”;
            }
        } 
        else 
        {
            return “No such contact”;
        }
    }
    // Only change code above this line
}

// Change these values to test your function
lookUpProfile("Akira", "likes");

As far as I can ascertain, I’ve followed the instructions to the letter. I’d appreciate any help.


#2

You have not declared firstName variable anywhere in code and it is neither in arguments.
Replace this firstName with name variable as you have in arguments.
See if that helps.


#3
    // Only change code below this line
    for (var i = 0; i < contacts.length; i++) 
    {
        if (name === contacts[i]["firstName"]) {
            if (contacts[i].hasOwnProperty(prop)) {
                return contacts[i][prop];
            } 
            else 
            {
                return “No such property”;
            }
        } 
        else 
        {
            return “No such contact”;
        }
    }
    // Only change code above this line
}

#4

Remove this else statement.

Put this line after the for loop:

That should help.


#5

@SeanFl42 - @aditya_p gave you some advice, but can you explain why return “No such contact” should go after the for loop?


#6

Hi. Is it because “No such contact” should be returned once i is no longer less than the length of contacts?


#7

Yes, but why is that significant? What does it mean with the respect to the algorithm when i is no longer less than the length of the contacts array? In other words, why should you not return “No such contact” when i is less than the length of contacts?


#8

To attempt to answer your second question, is it because i < contacts.length means there are still contacts to run through before you reach the end of the contacts list?


#9

I think you are understanding what my point is. Just in case, my point is if you iterate all the way through contacts without one of the other return statements executing (i.e returning a prop value or returning “No such property”, you can be certain that the name passed to the function does not belong to any of the objects in the contacts array. If the name would have matched, then one of the other return statements inside the for loop would have executed and the line return “No such contact” would have never been reached/executed.


#10

Thank you @RandellDawson and @aditya_p for your help.