简体   繁体   English

如何在函数中编写if / else语句,该函数告诉我数字是否可以被2整除(在javascript中)

[英]How do I write an if/else statement inside a function that tells me if a number is evenly divisible by 2 (in javascript)

I'm working through codacademy and I can't understand the help discussions in the forum. 我正在通过codacademy工作,但我听不懂论坛中的帮助讨论。

This is what I have so far but it returns false when i run the function with an even number: 到目前为止,这是我所拥有的,但是当我使用偶数运行函数时,它返回false:

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

You are performing the mathematical operation on isEven (the function itself). 您正在对isEven (函数本身)执行数学运算。 You need to check number : 您需要检查number

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

or better yet: 或更好:

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

You could even do this, by making use of the truthy/falsy behavior of 1 and 0 : 您甚至可以通过利用10的真实/虚假行为来做到这一点:

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

but I think the previous approach more clearly conveys how the logic works. 但是我认为以前的方法更清楚地传达了逻辑的工作原理。

you can write this function as follows: 您可以如下编写此函数:

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

Your function is not working because you're checking against the function name, not the function parameter (number). 您的函数无法正常运行,因为您要检查的是函数名称,而不是函数参数(数字)。 Try this: 尝试这个:

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

You doing it wrong in isEven itself.. Do it like.. 您在isEven本身中做错了。

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

Consider the following: 考虑以下:

  • 0 is considered to be false in Javascript. 在Javascript中0被认为是false的。
  • 1 is considered to be true in Javascript. 1在Javascript中被认为是true的。
  • ! makes false become true and vice versa. 使false变为true ,反之亦然。
  • The % operator can be used to keep higher numbers in a zero-to-one range. %运算符可用于将较高的数字保持在零到一的范围内。

Since 0 % 2 gives 0 (ie false ) and 1 % 2 gives 1 (ie true ), you simply need to invert the result with ! 由于0 % 2给出0 (即false ),而1 % 2给出1 (即true ),因此您只需要使用!求反! :

function isEven(x) { return !(x % 2); }

console.log(isEven(0));  // true
console.log(isEven(1));  // false
console.log(isEven(2));  // true
console.log(isEven(3));  // false

Note that this could also be written as: 请注意,这也可以写成:

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

...because: ...因为:

  • 0 % 2 is 0 (so it is true to say that it is equal to zero). 0 % 20 (因此它是true地说,它是等于零)。
  • 1 % 2 is 1 (so it is false to say that it is equal to zero). 1 % 21 (因此说它等于零是false )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 javascript如果数字可以被整除,则为true;否则为true - javascript if number is evenly divisible true if not false 我如何在if ... else语句中调用函数? - How do I call a function inside a if…else statement? 如何在数组JavaScript中找到均等的值? - How to find evenly divisible values in array javascript? 如何在JavaScript中编写if else语句 - how to write an if else statement in javascript 如何生成一个可以被另一个随机生成的数整除的随机数 - How to generate a random number that is evenly divisible by another randomly generated number 如何使用 if/else 语句编写 javascript 函数来检查是否<div>类包含一个<img>或者<img>班级 - How can I write a javascript function with the if/else statement to check whether a <div> class contains a <img> or <img> class 如何使用JavaScript在Imacros中的for循环内创建if else语句 - How do I create an if else statement inside a for loop in Imacros using javascript 如何在单引号内写if和else语句? - how to write if and else statement inside single quote? 每次调用函数(JavaScript)时,如何检查if / else语句? - How do I make the if/else statement get checked every time my function is called (JavaScript)? 如何缩短 every() 回调 function 中的 if else 语句? - How can I shorten if else statement inside every() callback function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM