gpt4 book ai didi

javascript - if 语句不返回 true

转载 作者:数据小太阳 更新时间:2023-10-29 05:02:16 24 4
gpt4 key购买 nike

我应该得到一个可以被 2 整除的数字,而我正在这样做。我不确定为什么我的代码不起作用。我在学习 javascript 的类(class)中​​这样做。我得到的错误是:

Oops, try again. Looks like your function returns false when number = 2. Check whether your code inside the if/else statement correctly returns true if the number it receives is even.

问题是这样的:

Write an if / else statement inside the isEven function. It should return true; if the number it receives is evenly divisible by 2. Otherwise (else), it should return false;. Make sure to return - don't use console.log()!

我的代码

var isEven = function(number) {
// Your code goes here!
if(4 % 2) {
return true;
} else {
return false;
}
};

我做错了什么?

最佳答案

试试这个

var isEven = function(number) {
if(number % 2 === 0) {
return true;
} else {
return false;
}
};

console.log(isEven(4));
console.log(isEven(3));
console.log(isEven(6));

一行更美

var isEven = function(number) {
return number % 2 === 0;
};

console.log(isEven(4));
console.log(isEven(3));
console.log(isEven(6));

关于javascript - if 语句不返回 true,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33654289/

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