Trouble with 'profile lookup'

So I have the code below, the first if statement reads prop key value if name and prop match, second return the string if the name doesnt match and the third returns the string if the name matches but not the prop.

if I take the bottom 2 if statements out, the top if statement returns correctly. If I have the bottom 2 statements in, the bottom 2 work but the top one doesnt.

function lookUpProfile(name, prop) {

  // Only change code below this line

for (let i = 0; i < contacts.length; i++){

if (contacts[i].firstName == name && contacts[i].hasOwnProperty(prop)) {

return contacts[i][prop];

} else if (contacts[i].firstName != name) {

  return "No such contact";

} else if (contacts[i].firstName == name && contacts[i].hasOwnProperty(prop) == false) {

  return "No such property";

}

}

}

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 it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

That is really useful thank you, i didnt think it looked very readable but wasnt sure what to do

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

I like using indentation to help show the logic.

With the indentation you can more easily see that you return “No such contact” inside of the loop over all contacts. But a return statement immediately stops and exits your function. So can you say inside of this loop that every single contact does not match?

That makes sense now thank you, I was thinking that it would have ran all of the [i] values through the first if statement and returned if true, and then ran all of the [i] values through the second statement etc. But I put an example through the function and could see why it would have returned the 2nd or 3rd value before it got to the correct value. I have solved it by running the loop on the first if statement, and running a second loop on the 2/3rd statements which would only activate if the first statement didnt ‘return’. I havent looked at the answer yet which i will do now, I am sure there is a much simpler way! Thanks again.