Profile Lookup- what's wrong with my code?

Hi! This is my code:

function lookUpProfile(firstName, prop){
// Only change code below this line

for (var x = 0; x < contacts.length; x++) {

  if (contacts[x].firstName == firstName) {
    
      if  (contacts[x].hasOwnProperty(prop)){
        return contacts[x][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(“Harry”, “likes”);

It’s not working with “Harry” - why? Explain me pls.

YOU ARE SO CLOSE!!

Think about when you want to return “No such contact”. What happens when x === 0, and what happens when a loop returns a value?

3 Likes

Thanks, now I see my mistake! I’ll never get x ===1 because loop returns “No such contact” after first iteration instead of last. I need move 'return “No such contact” ’ out of the loop!

2 Likes

I had exactly the same mistake and it drove me crazy until I found it

1 Like