Profile Lookup challenge nested if works but && doesn't

Hi,

Sorry for the stupid question because I can see people have answered on here but I’m struggling to understand the answer haha

I don’t understand why nested if works but using && doesn’t work in this challenge.

If anyone can point me in the right direction I would really appreciate it.

Thanks

Simon

Please post your code and a link to the challenge that you are working on.

Hi Jeremy

Thanks for the reply.

So the function with the nested if works but the one with && doesn’t.

// Setup 

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(name, prop) { 

  // Only change code below this line 

  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"; 

} 

 
 

lookUpProfile("Akira", "likes"); 

However this one doesn’t when I replace the nested if with &&

function lookUpProfile(name, prop) {
  // Only change code below this line
  for (var i = 0; i < contacts.length; i++) { 

    if (contacts[i].firstName === name && contacts[i].hasOwnProperty(prop)) { 

        return contacts[i][prop]; 

      } else { 

        return "No such property"; 

    } 

  } 

  return "No such contact"; 

} 

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

Well, the logic between the two is completely different.

In this case, the else occurs whenever contacts[i].firstName === name && contacts[i].hasOwnProperty(prop) is false, which is when contacts[i].firstName === name is false OR contacts[i].hasOwnProperty(prop) is false.

Thanks for this.

So for example for name I put Harry and prop as lastname.

They would both be true?

Sure, but if you have Harry and pets what will happen? What should happen instead?

Thanks, when I do Harry and lastName it returns no such property when using &&. I thought they both would be true.

What I noticed when looking at a debugger was with the nested if statement it checks only the name through the loop and then when it matches it moves on to check the prop

ah so Akira, likes works with the &&, it must be that it never goes beyond 0 when using the loop with &&

Pause for a second. Remember that

  1. You are using a loop over all contacts

  2. return forces your function to stop immediately

This is why I asked what would happen with Harry and pets. Same thing is happening with Harry and lastName. Your && doesn’t do what you think it does.

Appreciate the help. I think I understand it. Akira and likes works because i is 0 so they both are true. Harry and likes doesn’t work because when i is 0 it is false but prop would be true so it would move on to No such property. I feel like I am still missing something though haha

The very first time your code hits this line, your function immediately stops and no other contacts are checked.

Thanks, one last question. In the nested if solution that works when I put name as Harry how comes when i is 0 at the beginning and it is false it doesn’t move on to the else section?

In this case, the two return statements are only encountered when contacts[i].firstName === name.

Thanks, sorry I know I am missing the obvious here.

Why does that happen. Why does the loop keep going through the contacts even though in the first instance when I put Harry as name it is false.

if (someTestCondition) {
  // WHEN THE TEST CONDITION IS FALSE
  // NONE OF THIS CODE RUNS
}

When the condition if false, nothing inside of the if statement runs. That is the entire reason if clauses exist, to make sure code only is executed when the if condition is met.

In the context of your code

when contacts[i].firstName === name is false, nothing inside of that if clause runs. You do not check if the property matches if the name does not match.

Thanks I appreciate your help. I still don’t really get it but I will keep going until I do. I understand if statements and the for loop I just don’t get why the && doesn’t work beyond akira which is 0.

The loop goes through all the contacts so when i is 1 and contacts[1].firstName will be Harry and contact[1].hasOwnProperty(lastName) will be true then how comes no such property is return.

With an and

if (firstCondition && secondCondition) {
  // THIS CODE ONLY RUNS IF BOTH ARE TRUE
} else {
  // THIS CODE RUNS IF EITHER ARE FALSE
}

The else clause always executes if the if clause is false. With an &&, the if condition will be false if either the name does not match OR the property does not exist.

I think you might be missing how boolean logic works.


// What different outcomes occur when you change a and b?
const a = true;
const b = false;

if (a && b) {
  console.log("both a and b are true");
} else {
  console.log("a is false or b is false, or both are false");
}

How would this table fill out?

a b a && b
true true
true false
false true
false false

I am going to take some time away to think about all of this and come back to you haha I am sure I will get it soon. Hope you have a good rest of the day :smiley:

a b a && b
true true “both a and b are true”
true false “a is false or b is false, or both are false”
false true “a is false or b is false, or both are false”
false false “a is false or b is false, or both are false”