Basic JavaScript - Profile Lookup

Should this not work with &&

if (contacts[i].firstname === name && contacts[i].hasownproperty(prop)){
return contacts[i][prop];
}

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

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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:120.0) Gecko/20100101 Firefox/120.0

Challenge Information:

Basic JavaScript - Profile Lookup

You have to be careful about capitalization. For starters, firstname should be firstName. You have more bugs to fix after that, but that’s a start.

3 Likes

Some suggestions/improvements for your code:

  1. In modern JS, it preferred to use the const for constants, and let for variables rather than var.

It’s important that the style of variable’s name is case-sensitive which means the capitalization of the name should be remained.

Since every if-else statement has its return, then you simply return the result at the end without else statement.

If the code still not works, check the conditions and the requirements or the syntaxes again in case you missed something.

Hope it helps.

3 Likes

Thank you for being in detail , I was confused and thought the approach was incorrect.

1 Like

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