Javascript why is this not working

my code fails the test and I do not see why ?


// 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 -1; i++){
   if (contacts[i].firstName === name && contacts[i].hasOwnProperty(prop)) {
     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 (Windows NT 10.0; Win64; x64; rv:99.0) Gecko/20100101 Firefox/99.0

Challenge: Profile Lookup

Link to the challenge:

Which test does it fail?


When can you conclude that no contacts match?

for example these two tests :

lookUpProfile("Kristian", "lastName") should return the string Vos
lookUpProfile("Sherlock", "likes") should return ["Intriguing Cases", "Violin"]

So… when can you say that no contacts match?

if there are no matches from the for loop.
I changed that already but still those tests fail

new code :

function lookUpProfile(name, prop) {
  // Only change code below this line
 for (let i = 0; i <= contacts.length -1; i++){
    if (contacts[i].firstName === name && contacts[i].hasOwnProperty(prop)) {
      return contacts[i][prop];
    } else {
      return "No such property";
    }
 
 }
  return "No such contact";
   
  // Only change code above this line
}

Now lets look at this part

When will this be true? When will this be false?

it will be true if the given name is equal to the firstname of a person and if that person has that property.

And under what conditions will it be false?..

if a person has not that name or if that person has not that property.

So it looks i cannot use a && here

There are lots of ways to fix it, but you’ve got the core of the issue.

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