My code is bad?

Tell us what’s happening:
Why in the final step

console.log(lookUpProfile("Akira", "address"));

Don’t show my message “No such property”

  **Your code so far**

// Setup
const contacts = [
{
  firstName: "Akira",
  lastName: "Laine",
  number: "0543236543",
  likes: ["Pizza", "Coding", "Brownie Points"],
},
{
  firstName: "Harry",
  lastName: "Potter",
  number: "0994372684",
  likes: ["Hogwarts", "Magic", "Hagrid"],
},
{
  firstName: "Sherlock",
  lastName: "Holmes",
  number: "0487345643",
  likes: ["Intriguing Cases", "Violin"],
},
{
  firstName: "Kristian",
  lastName: "Vos",
  number: "unknown",
  likes: ["JavaScript", "Gaming", "Foxes"],
},
];

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


console.log(lookUpProfile("Kristian", "lastName"));
console.log(lookUpProfile("Sherlock", "likes"));
console.log(lookUpProfile("Harry", "likes"));
console.log(lookUpProfile("Bob", "number"));
console.log(lookUpProfile("Bob", "potato"));
console.log(lookUpProfile("Akira", "address"));
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36

Challenge: Profile Lookup

Link to the challenge:

Hi @FernandoBL !

That’s because the first condition here is true for that test case.

Since that is true then it executes that return and ends the function.
You will never get to that last else if in this case.

You will need to rethink parts of this algorithm to get all of the test cases to work.

1 Like

Thanks, I rethink my algorithm and I can. THANK U.

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

Congrats on solving it. :grinning:
That challenge is tricky for most people.

A couple of things to consider for the future.

No1:
You don’t have to explicitly say is true here

When you think about it, either that condition will be true or false.
It is a little redundant to say == true :grinning:

if(contacts[i].hasOwnProperty(prop))

No.2:
You actually don’t need that last condition at all.

You can just return "No such contact" after the for loop.

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

Hope that helps! :grinning:

1 Like