Basic JavaScript: Profile Lookup someone please help what i am doing wrong

Please someone explain what i am doing wrong. I thought for loop will iterate for each i and check if statement.

Your code so far


// Setup
var 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(var i = 0; i < contacts.length; i++){
  if(contacts[i]["firstName"] == name && contacts[i].hasOwnProperty(prop) == true){
    return contacts[i][prop];
  }
  else{
    return "No such property";
  }
}
return  "No such contact";
// Only change code above this line
}

lookUpProfile("Akira", "likes");

Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36.

Challenge: Profile Lookup

Link to the challenge:

You are returning out of the loop before you have checked all the objects inside the array.

Try logging console.log(contacts[i]["firstName"]); at the top of the for loop, think about what happens when the condition is not true.

Example
function lookUpProfile(name, prop) {
  // Only change code below this line
  for (var i = 0; i < contacts.length; i++) {
    console.log(contacts[i]['firstName']); // Akira

    if (
      contacts[i]['firstName'] == name &&
      contacts[i].hasOwnProperty(prop) == true
    ) {
      return contacts[i][prop];
    } else {
      return 'No such property';
    }
  }
  return 'No such contact';
  // Only change code above this line
}

console.log(lookUpProfile('Kristian', 'lastName')); // No such property

@parthi-rp you will need to first check if a contact with the firstName exists, if yes, then check for contacts property to see if such property exists.

your code is correct, it doesn’t loop through all elements in the contacts array because the first element already matches the condition in your if statement so it returns