Coming from intermediate python background, I just started learning javascript and I find it hard to figure out what's wrong with my code. I got the expected result with python but not with javascript.
The question
Write a program that uses console.log to print all the numbers from 1 to 100, with two exceptions. For numbers divisible by 3, print "Fizz" instead of the number, and for numbers divisible by 5 (and not 3), print "Buzz" instead.
When you have that working, modify your program to print "FizzBuzz", for numbers that are divisible by both 3 and 5 (and still print "Fizz" or "Buzz" for numbers divisible by only one of those).
Code in python
for x in range(100):
if x % 3 == (x % 5) == 0:
print('FuzzBazz')
if x % 3 == 0:
print('Fizz')
continue
if x % 5 == 0:
print('Buzz')
continue
print(x)
Code in JS
for (var x = 0; x < 100; x++) {
if (x % 3 === x % 5 === 0) {
console.log("FizzBuzz");
}
if (x % 3 === 0) {
console.log("Fizz");
continue;
}
if (x % 5 === 0) {
console.log("Buzz");
continue;
}
console.log(x);
}
4 Answers 4
2 mistakes, both in the same line:
You forgot a
0after your first comparisonx%3You forgot to include the AND operator
&&in the line
Therefore
if(x%3===x%5===0)
should actually be:
if(x%3===0 && x%5===0)
Also, worth pointing out that else can be used instead of continue, like so:
if (x%3===0 && x%5===0) {
console.log("fizzbuzz");
} else if (x%3===0) {
console.log("fizz");
} else if (x%5===0) {
console.log("buzz");
} else {
console.log(x);
}
7 Comments
continue; in his JS, which isn't validcontinue is redundant in OP's case since execution will continue regardless.continue is not redundant, as it skip the rest of the loop; however, it is not the only way of accomplishing the goal either: else ifs should be used instead of subsequent ifs and an else should be added at the end to print the original number if none of the other conditions matchThe 2 mistakes are
- you have used
if(x%3===x%5===0)which is wrong because x%3===x%5 is true not 0 and this will be compared to 0 which will always be false so modify it to(x%3===0 && x%5===0). - since both multiples of 3 and 5 should be replaces with FizzBuzz it shd be in a if else loop other wise for
eg : 15 it will print FIZZBUZZ and FIZZ and BUZZ which is wrong.
I have modified the code please have a look at it.
for (var x=0; x<100; x++){
if(x%3===0 && x%5===0){
console.log("FizzBuzz");
}else{
if(x%3===0){
console.log("Fizz");
}if(x%5===0){
console.log("Buzz");
}
console.log(x);
}
}
1 Comment
continue will ensure it skips the other branchesTry below code
for (var x=0; x<10; x++){
if(x%3===0 && x%5===0){
console.log("FizzBuzz");
}
if(x%3===0){
console.log("Fizz"); continue;
}
if(x%5===0){
console.log("Buzz");continue;
}
console.log(x);
}
Comments
Your confusion comes from the fact that JavaScript handles comparisons slightly differently to Python.
When you run x%3===x%5===0, you're expecting JS to compare x%5 to 0 and x%3 to x%5 as Python does. However, JS compares x%3 to x%5, which results in either true or false, which it then compares to 0. Because neither true or false are equal to 0, that expression will always evaluate to false.
Your comparison expression directly translated to JS would look like x%3 == 0 && x%5 == 0. However, as 15 is the lowest common multiple of 3 and 5, that expression is identical to x % 15 == 0.
There has also been some discussion in comments and other answers as to whether continue is necessary or even legal in JS. The answer to both is "yes, it is", however there is a simpler method.
Rather than using continue, as in the following code:
for (var x = 0; x < 100; x++) {
if (x % 15 == 0) {
console.log("FizzBuzz");
continue;
}
if (x % 3 == 0) {
console.log("Fizz");
continue;
}
if (x % 5 == 0) {
console.log("Buzz");
continue;
}
console.log(x);
}
it is possible to use else if as in this code:
for (var x = 0; x < 100; x++) {
if (x % 15 == 0) {
console.log("FizzBuzz");
} else if (x % 3 == 0) {
console.log("Fizz");
} else if (x % 5 == 0) {
console.log("Buzz");
} else {
console.log(x);
}
}
This final piece of code is how I would implement fizzbuzz in JavaScript.