Profile Lookup *HELP*

Hey, i’m trying to do this exercise, when i hit run code, it doesn’t work. i want to know why my code is wrong? when i compare it to the correct answer the only difference is mine a bit wordy. why it doesn’t work?

for (let i = 0; i < contacts.length; i++) {
  if (contacts[i].firstName !== name) {
      return "No such contact";
    }
    else if (contacts[i].hasOwnProperty(prop) == false) {
    return "No such property";
    }
    else if (contacts[i].firstName == name) {
      if (contacts[i].hasOwnProperty(prop) == true) {
        return contacts[i][prop];
      }
    }
  }

OR this one

for (let i = 0; i < contacts.length; i++) {
  if (contacts[i].firstName == name && contacts[i].hasOwnProperty(prop) == true) {
        return contacts[i][prop];
        }
  else if (contacts[i].firstName !== name) {
        return "No such contact";
        }
  else if (contacts[i].hasOwnProperty(prop) == false) {
        return "No such property";
        }
}

‘return’ immediately stops the function and the loop doesn’t run any more iterations.

Note - your formatting is very irregular here.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.