'Nested if statements vs &&(or) operator

I've been trying to solve this https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/profile-lookup/ with the following code:

function lookUpProfile(name, prop){
for (let a = 0; a < contacts.length; a++) {
  if (contacts[a].firstName == name && contacts[a].hasOwnProperty(prop)) {
    console.log(contacts[a][prop]);
  }
    else if (name != contacts[a].firstName) {
      return "No such contact";
    }
     else {
       return "No such property";
     }
}

However this page https://guide.freecodecamp.org/certifications/javascript-algorithms-and-data-structures/basic-javascript/profile-lookup proposes the following, and it works:

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

I also tried modifying the above to this:

for (var x = 0; x < contacts.length; x++) {
    if (contacts[x].firstName === name && contacts[x].hasOwnProperty(prop)) {
            return contacts[x][prop];
        } else {
            return "No such property";
        }
    }
}
return "No such contact";

To no avail, though. So my question is, why doesn't my code work? And why is there the need to use nested if statements instead of && operator?

Thank you for your attention.



Solution 1:[1]

Using the function that you created will always return: contacts[x][prop]; or "No such property", and, this line return "No such contact"; will never be executed, Why ?

By nesting the two if statements, the new if statement that will fall in returning either: contacts[x][prop]; if it validates to true or "No such property" if it validates to false and thus this line will no longer be executed/accessible, even if the contacts[x].firstName === name condition validates to false. And that's why two if statements are used: the first to return "No such contact"; if it is false (even without an else statement, because nothing will be executed in the first if statement, if it validates to false of course, and thus, the function jumps to the next line after that if statement which is return "No such contact";)

Simply: your function will return "No such property" even if the contacts[x].firstName === name is falsy.

Here's a snippet to illustrate:

// some dummy values just for the demo !
var contacts = [
  {
    firstName: "ths",
    lastName: "sakh"
  },
  {
    firstName: "firstname",
    lastName: "lastname"
  }
];

/**
* The modified function
**/

var r = (function test(name, prop) {
for (var x = 0; x < contacts.length; x++) {
    if (contacts[x].firstName === name && contacts[x].hasOwnProperty(prop)) {
            return contacts[x][prop];
        } else {
            return "No such property";
        }
    }
  return "No such contact";
})("Wrong Name!", "firstName");
console.log('The modified function returned: "' + r + '" instead of return "No such contact"');

/**
* The original function
**/

var r = (function test(name, prop) {
  for (var x = 0; x < contacts.length; x++){
    if (contacts[x].firstName === name) {
      if (contacts[x].hasOwnProperty(prop)) {
        return contacts[x][prop];
      } else {
         return "No such property";
      }
    }
  }
  return "No such contact";
})("Wrong Name!", "firstName");
console.log('The original function returned: "' + r + '" and that\'s what should be returned.');

Hope I pushed you further.

Ps: don't panic by the syntax var r = (function(x, y){...}(arg1, arg2) that's an IIFE (aka (Immediately Invoked Function Expression)). Learn more about it.

Solution 2:[2]

If the input of the function is "Harry" who happens to be in the second or third place of the object, the loop won't reach because on the first iteration it will go to "No such contact" and stop running the function!

Solution 3:[3]

Each function you have posted has a unique behavior. The difference in the last two functions is that when you have contacts, in your modified function, if the expression you are testing with your if statement evaluates to false, "No such property" will be returned and you will only return "No such contact" in the case of having no contacts only.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ths
Solution 2 Showner91
Solution 3