Basic JavaScript - Profile Lookup

Hello there, kindly help me with the solution because I feel like my code should work perfectly but I’m getting 4/6.

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 && contacts[i].hasOwnProperty(prop)) {
return contacts[i][prop];
} 
else if (contacts[i].firstName === name && !contacts[i].hasOwnProperty(prop)) {
return "No such property";
}
else if (!contacts[i].firstName === name) {
  return "No such contact";
}
  }
  // 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/119.0.0.0 Safari/537.36 Edg/119.0.0.0

Challenge Information:

Basic JavaScript - Profile Lookup

change that to contacts[i].firstName !== name and those two errors will be solved

happy coding :slight_smile:

1 Like

Basically, you can return the “No such contact” without the else-if statement, this is the only case.

2 Likes

After changing that, my code is failing the first three tests and honestly I don’t understand why.

  • 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

What is your updated code?

2 Likes

Got it finally after returning “No such contact” without the else-if statement.
Here is the final working code

 for (let 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 && !contacts[i].hasOwnProperty(prop)) {
return "No such property";
}
  }
  return "No such contact";

I’d use some more conventional formatting so you can more easily see the scope of things:

  for (let 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 && !contacts[i].hasOwnProperty(prop)) {
      return "No such property";
    }
  }
  return "No such contact";
3 Likes

Better,
I’ve got also another more conventional and is also working perfectly

for (var i = 0; i < contacts.length; i++) {
    if (contacts[i].firstName === name) {
      if (contacts[i].hasOwnProperty(prop)) {
        return contacts[i][prop]; 
      } else {
        return "No such property"; 
      }
    }
  }
  return "No such contact";
1 Like

Looking good. Note though - var is considered a “legacy” feature so you should use let or const

1 Like

Well noted
Thank you.

1 Like

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