javascript/codecademy - if 语句在评估 true 之前运行两次 false?

标签 javascript if-statement logic

所以我是个新手,正在 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 and foodHere, and set them both equal to true. Inside the eat function, create an if statement that returns true only if both hungry and foodHere are true, and false otherwise. The function eat should take no input and hungry and foodHere should both be globals.

本质上,codecademy 获取了您的代码片段并运行了 eat() 几次,更改了 hungryfoodHere 的值,以确保eat() 针对说明中指定的条件返回适当的值。

你没有做错任何事。您的代码运行良好。

关于javascript/codecademy - if 语句在评估 true 之前运行两次 false?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25538626/

相关文章:

java - 如何使用表中的 if 函数在循环中排列重复字符串数组

Python 3 测试列表中的一系列项目

logic - 为什么这个循环在使用 && 而不是 'and' 时返回 1?

c++ - 如何从多索引中获取值

javascript - CSS 过渡高度

javascript - 使 HTML5 可拖动项目滚动页面?

java - 我想制作密码保护的android应用程序

javascript - 在 javascript 外部文件中加载/引用 css 文件

javascript - vue-style-loader 来自 cdn 的内联 css

haskell - 为什么 MLF 中有弹性和刚性边界?