Profile Lookup solution and nested if statements

Profile Lookup solution and nested if statements
0

#1

Hello,

I was stuck on Profile Lookup task, I googled solutions and all the results I found suggested nested if statements. Here is an example of the solution:

function lookUp( firstName, prop ){ for( var i = 0; i < contacts.length; i++ ){ if( firstName == contacts[i].firstName ) { if( contacts[i].hasOwnProperty( prop ) ) { return contacts[i][prop]; } else { return "No such property"; } } } return "No such contact"; }

But I don’t remember that nested if statements were discussed in any of the lessons. And I don’t know how they work. Can anyone suggest if I just missed this FreeCodeCamp lesson or explain how nested if statements work? I don’t want to simply copy this solution without understanding what it does.

Thank you!


#3

Also, sometimes a better alternative to nested if statements are Logical Operators which allow you to test multiple conditions in one if statement.

var a = 1;
if (a > 0 && a < 5) {
  console.log('In between 0 and 5!');
} else if (a < 0 || isNaN(a)) {
  console.log('Failure!');
}

#4

P1xt, thank you! That’s much easier than I thought it was.[quote=“P1xt, post:2, topic:37375”]
That special bit of code you only run when that certain thing is true, can be anything you want, even another if statement.
[/quote]


#5

IsaacAbrahamson, that’s true! I completely forgot about them. Thank you for your reply.