Why is && different from two nested if statements?

I did if (condition && condition) return. The answer says I should do if (condition) if (condition). Is there a reason why the && operator won’t work?

  **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 if (contacts[i][prop] != prop) {
    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) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36

Challenge: Profile Lookup

Link to the challenge:

because you are doing it wrong, your else ifs are within the for loop so in other words you for loop only gets to run once.

You need to first iterate through the array then make the call whether or not to do the conditions.

Yup, you’re looking at the first contact in the list, and if their name doesn’t match you immediately give up and say no one in this list has that name (even though you haven’t even checked yet).

Ah I get it now. I tried it again but I’m getting confused with all the brackets. Is there an easier way to manage the brackets?

Ah I see. Thank you!

Right click on the editor and select “Format document” so that your brackets all line up. When using an IDE like VS Code you can do things like match the colors of brackets or highlight the current indentation block, etc. Unfortunately the fCC editor is pretty bare bones.

I’ll try that. Thank you!

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