嵌套if语句vs &&(or)运算符

时间:2018-08-25 22:40:19

标签: javascript arrays object if-statement

我一直在尝试使用以下代码来解决https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/profile-lookup/

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

然而,此页面https://guide.freecodecamp.org/certifications/javascript-algorithms-and-data-structures/basic-javascript/profile-lookup提出了以下建议,并且可以正常工作:

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

我还尝试将上面的内容修改为此:

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

但无济于事。 所以我的问题是,为什么我的代码不起作用?为何需要使用嵌套的if语句而不是&&运算符?

感谢您的关注。

3 个答案:

答案 0 :(得分:0)

您发布的每个功能(关于最后两个功能)都有独特的行为。区别在于,当您拥有contacts时,在修改后的function中,如果您正在使用if语句测试的表达式的值为false,则"No such property"将返回,并且仅在没有"No such contact"的情况下,您只会返回contacts

答案 1 :(得分:0)

如果函数的输入是恰好位于对象第二或第三位的“哈利”,则该循环将不会到达,因为在第一次迭代时它将进入“无此接触”并停止运行功能!

答案 2 :(得分:0)

使用您创建的函数将始终返回:contacts[x][prop];"No such property",并且该行return "No such contact";将永远不会执行,为什么?

通过嵌套两个if语句,新的if语句将返回:contacts[x][prop];(如果它验证为true"No such property",则返回它验证为false,因此即使contacts[x].firstName === name条件验证为false,此行也将不再执行/不可访问。这就是为什么要使用两个if语句的原因:如果是"No such contact";,则第一个返回false(即使没有else语句,因为在第一个{ {1}}语句,如果它当然会验证到if,则该函数将跳到该false语句之后的下一行,即if

简单:即使return "No such contact";"No such property",您的函数也将返回contacts[x].firstName === name

下面是一段说明:

falsy

希望我进一步推动了你。

Ps:不要对// 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.');(又名var r = (function(x, y){...}(arg1, arg2))这样的语法IIFE感到恐慌。 Learn more

相关问题