gpt4 book ai didi

javascript - If/Else 逻辑顺序 - Javascript 示例 - 请解释这两个代码块之间的区别

转载 作者:行者123 更新时间:2023-11-28 17:48:26 25 4
gpt4 key购买 nike

我花了几个小时试图理解这一点 - 我知道它与 if...else 语句逻辑顺序有关,但每次我试图遵循时我都会迷失方向。请有人向我解释一下这一点,如果可能的话,请给出如何处理 if...else 逻辑顺序的指针(一种记住的黑客/快速方法,例如:“始终从较大的样本空间开始......”)

这里是:

The Task is as follows: We have an array of objects representing different people in our contacts lists.

A lookUpProfile function that takes firstName and a property (prop) as arguments has been pre-written for you.

The function should check if firstName is an actual contact's firstName and the given property (prop) is a property of that contact.

If both are true, then return the "value" of that property.

If firstName does not correspond to any contacts then return "No such contact"

If prop does not correspond to any valid properties then return "No such property"

代码

这段代码是错误的

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

for(var i=0;i < contacts.length;i++){

if (contacts[i].hasOwnProperty(prop))
{
if(contacts[i].firstName === firstName){
return contacts[i][prop];
}
else {
return "No such contact";
}

}
// Only change code above this line
}
return "No such property";
}
// Change these values to test your function
console.log(lookUpProfile("Harry", "likes"));

此代码是正确的:

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

for(var i=0;i < contacts.length;i++){

if (contacts[i].firstName === firstName)
{
if(contacts[i].hasOwnProperty(prop)){
return contacts[i][prop];
}
else {
return "No such property";
}

}
// Only change code above this line
}
return "No such contact";
}
// Change these values to test your function
console.log(lookUpProfile("Harry", "likes"));

如果有人能帮助我解释其中的区别,我将非常感激。预先感谢您!

最佳答案

这些代码的区别在于 if 条件的使用。在失败的代码中,如果用户名与参数传递的名称不匹配,您将从函数返回。因此,当第一项不匹配时,它将返回。

if (contacts[i].hasOwnProperty(prop))
{
//INCORRECT USE OF NAME CHECK IF CONDITION
if(contacts[i].firstName === firstName){
return contacts[i][prop];
}
else {
return "No such contact";
}

}

在第二个中,只有当第一个名称匹配时您才会返回。

//CORRECT USE OF IF CONDITION FOR NAME CHECK
if (contacts[i].firstName === firstName)
{
if(contacts[i].hasOwnProperty(prop)){
return contacts[i][prop];
}
else {
return "No such property";
}

}

关于javascript - If/Else 逻辑顺序 - Javascript 示例 - 请解释这两个代码块之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46182722/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com