- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我花了几个小时试图理解这一点 - 我知道它与 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/
这个问题在这里已经有了答案: Does an else if statement exist? (2 个答案) 关闭 4 年前。 相信大家在学习C的时候,都学过这样的语法: if (conditio
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
Java 11.6 在这个 BMI 计算器中,它将输入人的体重、高度并计算 BMI。 BMI 计算正确,但是在分类 BMI 方法中,程序会跳过 if 和 else if 检查,只打印出每次测试的“肥胖
我有以下代码,它根据 url 参数更改,然后隐藏表单上的选择选项。即 www.example.com?type=images 最终会有 20 多个不同的参数。我想知道比拥有大量 if else 更好的
在WikiPedia Binary Search 的文章中有一个名为Deferred detection of equality 的部分,其中介绍了二进制搜索的某种“优化”版本,如下所示: int b
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我在下面的代码中遇到错误。尝试了 if 和 elseif 的所有组合,但无法使其工作。 错误:未捕获的语法错误:意外的标记其他 if(disableAd == 0 && disableWeather
我是 Java 初学者,我正在做一些练习题来提高我的 Java 技能。 编写一个名为 season 的方法,该方法接受两个整数作为代表月份和日期的参数,并返回一个指示该月份和日期的季节的字符串。假设月
有没有可能在 JavaScript 中做这样的事情? if (name == 'foo') { exampleFunction('some_arg'); } else if (name ==
我有一段简单的代码来检查一些条件,我希望每个条件都产生一个唯一的输出,但如果没有满足条件,则产生另一个唯一的输出。 有什么方法可以创建仅在所有先前的 if 语句都失败时才触发的 else 吗?我知道下
关闭。这个问题是opinion-based 。目前不接受答案。 想要改进这个问题吗?更新问题,以便 editing this post 可以用事实和引文来回答它。 . 已关闭 8 年前。 Improv
我发现自己经常使用这样的模式: if (a > b) { foo(); } elseif (c > d) { bar(); } else { baz(); } 这里的要点是第二
Hive 使用 IF(条件,表达式,表达式),所以当我想做 if/else if/else if/else 时,我必须这样做: IF(a, 1, IF(b, 2, IF(c, 3, 4))) 有没有更
直到 if 和 else 具有共同的作用域: if (int x = foo()) { // .... } else if (int x = bar()) { // ... } else {
是否可以保证 if-else if-else if-else block 中的 ifs 会按照编写顺序进行测试。 我问这个是因为我经常尝试通过将最常见的情况放在首位来优化我的代码,我想知道编译器所做的
我有一个方法可以检查某些事情并根据这些检查返回一个 bool 值。它涉及一个单独的分支 If 部分,该部分按顺序检查大约 5 个条件。如果这些条件中的任何一个返回真,则该方法将返回真;。如果没有任何条
这个问题在这里已经有了答案: Yet Another Conditional Operator Nesting Question (2 个回答) Why is the conditional oper
有没有一种方法可以从一个事件运行多个 if else 语句?例如,尝试运行此命令,但它仅对类为“two”的元素执行第一个 if else,而不是对类为“三”的第二个元素执行。 $(".hamburge
我有一个类似这样的查询: SELECT SUM(`table`.points) AS total_points FROM table 这将返回类似这样的内容 | **points** |
我正在尝试对单选按钮组运行“存在检查”,以确定如果在组中选择了 2 个单选按钮中的 1 个(if 语句),如果选择了 2 个单选按钮中的另一个(否则 if语句)或者如果两者都没有选择(else 语句)
我是一名优秀的程序员,十分优秀!