Basic JavaScript - Profile Lookup

Tell us what’s happening:
please help me to find the mistake. I have checked the solution and it is the exact copy of the solution but still it fails the first three run tests.

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) {
    if (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 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.3 Safari/605.1.15

Challenge: Basic JavaScript - Profile Lookup

Link to the challenge:

You can add these line to before your return statement. It helps to see where your if-else logic goes.

console.log(contacts[i][prop])

console.log(“No such property”)

console.log(“no such contact”)

And i when i tried the first case, it told me No such contact.

Seems like your for-loop didnt go thru all objects in your contacts. i.e. Once the first object name NOT matched, the rest are skipped.

image

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