Profile Lookup lesson

hello,

can someone please explain why when i use and && with my answer it doesn’t work?

i do get “Akira” likes but i dont get to pass the lesson

var 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(firstName, prop){
// Only change code below this line
   for(x=0; x<contacts.length; x++){
     if(contacts[x].firstName == firstName && contacts[x].hasOwnProperty(prop)){
       return contacts[x][prop];
     }
     else{
     return "No such property";
     }
   }
  return "No such contact";
// Only change code above this line
}

// Change these values to test your function
lookUpProfile("Akira", "likes");

I’ve edited your post for readability. When you enter a code block into the forum, remember to precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

markdown_Forums

When you use the && operator in the if statement, both of the conditions must be true.

By looking at the test case of lookUpProfile("“Kristian”, “lastName”), you will see the problem. During the first iteration of the for loop, i = 0, so contacts[0].firstName is “Akira”. The firstName function argument is “Kristian”, so you already have a false condition because Akira is not equal to “Kristian”. Because the if statement evaluates to false, the else code block returns “No such property”, which is not the correct answer. Hopefully, now you can see the issue with your current solution’s logic.

1 Like

thanks Randel,

I thought that && and nested if are the same