Basic Javascript - profile lookup

Hello everyone! I thought I was being pretty clever with my solution, but it seems to have some fundamental flaw:

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

I’m not quite sure why this doesn’t work. I thought it might be because there were two different data types involved, but that doesn’t make a whole lot of sense… Any help would be appreciated!

Contacts is an array, but its an array that holds objects. How would you look at the key/value of an object? You also have returns inside your loops. The thing about returns is they will immediately stop and return what you tell them, even if you intend for that loop to keep going and look at everything in the array.

2 Likes

That is a very helpful answer! I think I understand now why I needed to use the .hasOwnProperty function. And if I’m understanding the loop issue correctly, on the innermost loop, I think it checked the ‘if’ property once, then goes to the ‘else’ of 'no such property, even though I wanted it to iterate through.

At least, I hope that’s what’s wrong with it. Either way, thank you for your answer! It has helped me learn more. I need to go back and review loops, at any rate.