0

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);
}
Arnold Gandarillas
4,4001 gold badge35 silver badges37 bronze badges
asked Jan 6, 2018 at 10:11
0

4 Answers 4

2

2 mistakes, both in the same line:

  1. You forgot a 0 after your first comparison x%3

  2. You 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);
}
squirl
1,8241 gold badge18 silver badges32 bronze badges
answered Jan 6, 2018 at 10:15
Sign up to request clarification or add additional context in comments.

7 Comments

Maybe also mention that he's used continue; in his JS, which isn't valid
Actually @Lissy I just looked it up and I think it is valid. Still, I would argue that continue 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 match
According to the problem the code is right, the only mistake is the condition.
@ArnoldGandarillas This code still outputs the number on Fizz and Buzz
|
1

The 2 mistakes are

  1. 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).
  2. 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);
 }
}
answered Jan 6, 2018 at 10:20

1 Comment

2 is incorrect. continue will ensure it skips the other branches
1

Try 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);
}
answered Jan 6, 2018 at 10:32

Comments

1

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.

answered Jan 6, 2018 at 10:49

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.