admin管理员组文章数量:1278854
Hi I've just started to learn JavaScript. I'm doing the exercises on /. You can read it below:
FizzBuzz is a children's game where you count from 1 to 20. Easy, right?
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".
"1, 2, Fizz, 4, Buzz"...and so forth
Let's start by using console.log to print out all of the numbers from 1 and 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.
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:
var i;
for (i = 0; i <=20; i++) {
console.log(i);
}
if (i / 3 = 0) {
console.log("Fizz")
}
Any help would be great.
Hi I've just started to learn JavaScript. I'm doing the exercises on http://www.codecademy./. You can read it below:
FizzBuzz is a children's game where you count from 1 to 20. Easy, right?
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".
"1, 2, Fizz, 4, Buzz"...and so forth
Let's start by using console.log to print out all of the numbers from 1 and 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.
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:
var i;
for (i = 0; i <=20; i++) {
console.log(i);
}
if (i / 3 = 0) {
console.log("Fizz")
}
Any help would be great.
Share Improve this question asked Feb 4, 2012 at 19:23 CraigCraig 691 gold badge4 silver badges10 bronze badges 1- Unless i is 0, i/3 cannot be 0. – kennebec Commented Feb 4, 2012 at 19:25
5 Answers
Reset to default 3You 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
Comparisons are done with the ==
(or ===
) operator, not =
. The latter is for assignments of values to variables or object properties.
To do your if
statement, then, you'd write this:
if (i / 3 === 0) {
// ...
}
I used ===
because it's usually the right thing; ==
has some weird semantics that can be really confusing before you've gotten the hang of JavaScript.
As @kennebec notes in a ment, you may want to think through your logic again. Perhaps you want to check whether the number is divisible by three. In that case, you'd want to use the modulus operator:
if (i % 3 === 0) {
// i is divisible by 3
}
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);
}
}
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");
}
}
Using the modulus operator will allow you to check if i is divisable with 3 or 5, as i/3 will only be 0 when i is 0. Hope this will help you proceed to your practice in javascript
Actually you're going to want something more like (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.
本文标签: javascriptUsing Math Functions in an IF statementStack Overflow
版权声明:本文标题:javascript - Using Math Functions in an IF statement - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741287719a2370369.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论