What is the problem my code for Profile Lookup?

Tell us what’s happening:

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].firstName === name){
    if(contacts[i].hasOwnProperty(prop)){
    return contacts[i][prop];
  } else {
    return "No such contact";
  }
  }
}
return "No such property";

// Only change code above this line
}

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

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36.

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

I think you just inverted the order of the messages.

Comment for an example where it finds a name, but not a prop.

function lookUpProfile(name, prop) {
  for (var i = 0; i < contacts.length; i++) {  // iterates through the array
 
    if (contacts[i].firstName === name) {      // has found a matching name
      if (contacts[i].hasOwnProperty(prop)) {  // has not found the prop
        return contacts[i][prop];              // doesn't run this line
      } else {                                 // runs the else statement
        return "No such contact";              // no such CONTACT?? Should be "No such property" =)
      }
    }
  }
  return "No such property";
}
1 Like

Thanks so much :slight_smile: can i ask a question?
why does line run ?

return contacts[i][prop]; // doesn’t run this line

I meant that this line won’t run in the example, because the if-condition is not met.
For the sake of the example, I was assuming a case which you have found a name, but not a prop. So the first if-condition is true (it has found a name and will run the block). The second if-condition is not true, it has not found the provideded prop. Hence, the program will skip this line in favor of the else-condition.

1 Like

Himm i understood, thanks :slight_smile: