所以我是个新手,正在 Codecademy 上参加进修类(class)。我的逻辑有问题吗?
// Declare your variables here!
var hungry = true;
var foodHere = true;
var eat = function() {
// Add your if/else statement here!
if(hungry && foodHere){
console.log("problem solved!");
return true;
}
else {
console.log("why are you even here?");
return false;
}
}
没有参数摄入的函数会检查两个真实的语句,这两个语句是全局的和预定义的。当我保存并运行此脚本时,输出为
why are you even here?
why are you even here?
problem solved!
这对我来说,它两次评估为 false(尽管不应该),继续运行,最后评估为 true,让我可以继续前进。其次,如果我将一个或两个变量设置为 false,它会输出...什么也没有。没有评估,就没有输出。
这里是否存在我没有看到的问题,或者 Codecademy 控制台中出现故障?我认为检查报表很慢?我宁愿在继续之前弄清楚这一点。
最佳答案
来自列出的 Codecademy 说明 here :
Create two variables,
hungry
andfoodHere
, and set them both equal totrue
. Inside theeat
function, create anif
statement that returnstrue
only if bothhungry
andfoodHere
are true, andfalse
otherwise. The functioneat
should take no input andhungry
andfoodHere
should both be globals.
本质上,codecademy 获取了您的代码片段并运行了 eat()
几次,更改了 hungry
和 foodHere
的值,以确保eat()
针对说明中指定的条件返回适当的值。
你没有做错任何事。您的代码运行良好。
关于javascript/codecademy - if 语句在评估 true 之前运行两次 false?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25538626/