Profile Lookup--I can't find the error

Profile Lookup--I can't find the error
0.0 0

#1

Tell us what’s happening:
I tried doing the prop search in this exercise two ways 1. a nested for-loop(commented out) 2. hasOwnProperty method. The name check goes through and returns correctly if the name is not found, but the inner check for prop is not catching. I can’t see the bug. It seems like it should work.

Your code so far


//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 (contacts[i][0] == name){
       // for (var j = 0; j < contacts[i].length; j++){
      //      if (contacts[i][j] == prop){
           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", "address");

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/11.1.1 Safari/605.1.15.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/profile-lookup


#2

Perhaps you want to return “No Such Contact” once you are done looping and you are sure that nothing matched the search criteria…

Instead of returning it if contacts[i][0] !== name


#3

Everything is good with your code except you need to return “No such contact” after the search (for loop) is done :wink:


#4

I moved it outside of the for loop, and its still giving me the same errors. The “No such contact” are actually the only cases that are returning correctly, so I know that it is something in the inner codes…

function lookUpProfile(name, prop){

for (var i = 0; i < contacts.length; i++){
if (contacts[i][0] == name){
if (contacts[i].hasOwnProperty(prop)){
return contacts[i][prop];
}
else
return “No such property”;
}
}
return “No such contact”;
}


#5

Unless its all returning “No such contact”. Back to the editor I go.


#6

You missed some brackets. Otherwise it works. This is the working solution.

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