Profile Lookup module

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

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

// Only change code above this line
}

This is my first contact with programming of any kind. But I tried to do a different way than the hints page for this prompt. However I can only get “No such contact” to work with what I’ve written. Can anyone help me out and tell me what part of what I wrote is wrong?

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

Your code formatted:

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

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

  // Only change code above this line
}

Can you see that no matter which branch of the if/else you go down, you return from the function?

Thanks for the reply, but no I don’t not see it. Could you clarify a bit more please?

I do see that I have understood/used the “||” wrong and it will cause the function to always return “no such contact”.

Does the “else” statement always run in the way I wrote it? Is that what you meant?

Your issue’s not with ||, it’s with when to return “No such contact”.

Right now the flow of your logic looks like this:

check contacts[0]
  if name doesn't match:
    return "No such contact"
  else if contact doesn't have prop:
    return "No such property"
  else:
    return value of prop in contact

check contacts[1] <-- Your code never gets here

Since you’re guaranteed to return within any of the if/else branches of the first loop, you’re never going to check any contact beyond the first. You can’t say there’s “No such contact” until you’ve checked every contact to confirm there’s no match.

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