javascript 在 IF 语句中使用数学函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9143757/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using Math Functions in an IF statement
提问by Craig
Hi I've just started to learn JavaScript. I'm doing the exercises on http://www.codecademy.com/. You can read it below:
嗨,我刚刚开始学习 JavaScript。我正在http://www.codecademy.com/上做练习。你可以在下面阅读:
FizzBuzz is a children's game where you count from 1 to 20. Easy, right?
FizzBuzz 是一款从 1 数到 20 的儿童游戏。很简单,对吧?
Here's the catch: instead of saying numbers divisible by 3, say "Fizz". And instead of saying numbers divisible by 5, say "Buzz". For numbers divisible by both 3 and 5, say "FizzBuzz".
这里有一个问题:不要说可以被 3 整除的数字,而是说“Fizz”。与其说能被 5 整除的数字,不如说“Buzz”。对于可以同时被 3 和 5 整除的数字,请说“FizzBuzz”。
"1, 2, Fizz, 4, Buzz"...and so forth
“1, 2, Fizz, 4, Buzz”……等等
Let's start by using console.log to print out all of the numbers from 1 and 20.
让我们首先使用 console.log 打印出 1 和 20 中的所有数字。
But don't type out the numbers in order—find a more awesome way!
但是不要按顺序输入数字——找到更棒的方法!
I can get the numbers to print using a for loop but now I don't know how to replace the numbers with strings.
我可以使用 for 循环打印数字,但现在我不知道如何用字符串替换数字。
I thought about doing an IF statement like "if i divided by 3 is zero, then print Fizz" but I'm not sure how to do it. See what I've done so far below:
我想过做一个 IF 语句,比如“如果我除以 3 是零,然后打印 Fizz”,但我不知道该怎么做。看看我在下面做了什么:
var i;
for (i = 0; i <=20; i++) {
console.log(i);
}
if (i / 3 = 0) {
console.log("Fizz")
}
Any help would be great.
任何帮助都会很棒。
回答by
You need to test the remainder with the modulus operator.
您需要使用模运算符测试余数。
if (i % 3 === 0) {
The modulus operator returns the remainder after dividing the left operand by the right.
取模运算符返回左操作数除以右操作数后的余数。
12 % 3; // 0
13 % 3; // 1
14 % 3; // 2
15 % 3; // 0
回答by Pointy
Comparisons are done with the ==
(or ===
) operator, not =
. The latter is for assignments of values to variables or object properties.
比较是使用==
(or ===
) 运算符完成的,而不是=
. 后者用于将值分配给变量或对象属性。
To do your if
statement, then, you'd write this:
为了做你的if
陈述,然后,你会写这个:
if (i / 3 === 0) {
// ...
}
I used ===
because it's usuallythe right thing; ==
has some weird semantics that can be really confusing before you've gotten the hang of JavaScript.
我使用===
它是因为它通常是正确的;==
有一些奇怪的语义,在您掌握 JavaScript 之前可能会非常令人困惑。
As @kennebec notes in a comment, you may want to think through your logic again. Perhaps you want to check whether the number is divisibleby three. In that case, you'd want to use the modulus operator:
正如@kennebec 在评论中指出的那样,您可能需要再次思考您的逻辑。也许您想检查这个数字是否可以被 3整除。在这种情况下,您需要使用模运算符:
if (i % 3 === 0) {
// i is divisible by 3
}
回答by Kobe Bryan
var i;
for(i=1; i<=20; i++) {
console.log(i);
if(i / 3 === 0) {
console.log("Fizz");
}
else if(i / 5 === 0) {
console.log("Buzz");
}
}
Hope this will help you proceed to your practice in javascript
希望这能帮助您继续练习 javascript
回答by Debonemo
This works:
这有效:
for (i=1; i<21; i++) {
if(i%3 === 0 && i%5 !== 0) {
console.log("Fizz");
}
else if (i%5 === 0 && i%3 !== 0) {
console.log("Buzz");
}
else if (i%5 === 0 && !i%3 === 0) {
console.log("FizzBuzz");
}
else {
console.log(i);
}
}
回答by Letrstotheprez
Actually you're going to want something more like (i % 3 == 0)
.
实际上,您会想要更像(i % 3 == 0)
.
I'm not very familiar with JavaScript so it might be a different operator, if that's the case, %
means modulus. It returns the remainder of dividing to numbers. So in your case, let's say i
is 3
. Then i / 3 == 1
, but i % 3 == 0
. If i
was 6
, i / 3 == 2
, but i % 3 == 0
still.
我对 JavaScript 不是很熟悉,所以它可能是一个不同的运算符,如果是这种情况,则%
表示模数。它返回除法的余数。所以在你的情况下,假设i
是3
。然后i / 3 == 1
,但是i % 3 == 0
。如果i
是6
,i / 3 == 2
,但i % 3 == 0
仍然。