Profile Lookup troubles

Profile Lookup troubles
0

#1

Can anyone please tell me what is wrong with my code? I have been sitting here for a few hours trying to figure out why I can’t pass.


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

// Only change code above this line
}

#2

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make easier to read.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.


#3

Remember, as soon as a return statement executed, the function exits even if not finished iterating though a loop. Because of this fact, you for loop only makes a single iteration (when i = 0). During this iteration, you return one of three values and then the function exits.

In the test case of lookUpProfile(“Kristian”, “lastName”), when i = 0, contacts[i].firstName is “Akira”. Since “Kristian” is not euqal to “Akira”, your code returns “No such contact”, but it should really continue iterating through the objects in contacts. Think about where you could move the return “No such contacts” and guarantee all the objects have been checked.


#4

Thanks a bunch for the quick reply. I refactored my code and adjusted the return statement like you suggested, works great now!