I'm stuck, don't see difference between my code and solution

Tell us what’s happening:
This should work? Or did I make a mistake? I keep trying, I looked at the solution to understand it but don’t know why this isn’t working. The only difference is that I used ‘&&’ in the first if-lus instead of using 2 if-lusses. Someone?

  **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){
       return "No such contact";
     } else{return "No such property";}
// Only change code above this line


console.log(lookUpProfile("Kristian", "lastName"));
console.log(lookUpProfile("Sherlock", "likes"));
console.log(lookUpProfile("Harry", "likes"));
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.99 Safari/537.36

Challenge: Profile Lookup

Link to the challenge:

Hi @elkaddouri.od !

The answer to your question of why your code is not working can be answered by looking at the results from one of the test cases.

Take a close look at this test case here
lookUpProfile("Kristian", "lastName") should return the string Vos

Now let’s walk through your code, to see what is going on.

We enter our for loop, and check this first condition here

If we look at the first object here

we can see that the first name of Akira does not match the name of Kristian

So according to your code the next step would be to check this condition here

Since this condition is true, we return No such contact and exit the function.

The problem with your code is that you are only checking the first object and see if the names match.
You need to check the entire object and see if the first name can be found.
If so, then you check if the property exists.
If you have checked the entire object and the first name does not exist, then you return no contact.

Hope that makes sense!


Thank you :heart: :heart: :smiling_face_with_three_hearts:

I understand, it doesn’t go throught all the objects. It stopped after the first one. Thanks @jwilkins.oboe !

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