Record Collection code test

Can anyone tell me the problem with my code. Thanks.

  **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(firstName, prop) {
// Only change code below this line{
  var value;
  for (var i = 0; i < contacts.length; i++){
    if((contacts[i].firstName === firstName) && 
       (contacts[i].hasOwnProperty(prop))){
      value = contacts[i][prop];
      break;
  }
  if (contacts[i].hasOwnProperty(firstName) === false) {
    value = "No such contact";
  }
  if (contacts[i].hasOwnProperty(prop) === false) {
    value = "No such property";
  }
  }
  return value;
}
  

// Only change code above this line


lookUpProfile("Akira", "likes");
  **Your browser information:**

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

Challenge: Profile Lookup

Link to the challenge:

can you explain what logic you are following for your code? cna you describe what it is doing?

i have a lookUpProfile function that takes name and a property ( prop ) as arguments.

The function checks if name is an actual contact’s firstName and the given property ( prop ) is a property of that contact.

If both are true, then return the “value” of that property.

If name does not correspond to any contacts then return the string No such contact .

If prop does not correspond to any valid properties of a contact found to match name then return the string No such property .

But you have some funny logic for setting that value outside of your loop. Why are you using i outside of the loop?

I recommend always using let and const instead of var. That will prevent you from making one of your current logic errors.

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