Basic JavaScript - Profile Lookup (pass task 3/6 only)

My code can pass the last three tasks about:

  1. No such contacts
  2. No such property

But can’t do those three tasks well.

  • lookUpProfile("Kristian", "lastName") should return the string Vos
  • Failed:lookUpProfile("Sherlock", "likes") should return ["Intriguing Cases", "Violin"]
  • Failed:lookUpProfile("Harry", "likes") should return an array

I think there is something wrong in 34 lines return contacts[i][prop];, but I can’t get it.

I checked several times but couldn’t find the bug. Please help.
Thank you so much.

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 (name === contacts[i].firstName) {
  if (contacts[i].hasOwnProperty(prop)) {
    return contacts[i][prop];
}
    else {
    return "No such property";
}
}
else {
  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/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36

Challenge: Basic JavaScript - Profile Lookup

Link to the challenge:

else {
  return "No such contact";
}

What would happen for the first iteration?
It would stop the looping and end your function call immediately.

if you wish to end the loop : try break; statement
if you wish to skip the remaining item in the loop, and goto next loop : try continue; statement;

Happy coding

1 Like

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