# 3. ... And the good!

#1

Print out the numbers from 1 - 20.
The rules:
For numbers divisible by 3, print out "Fizz".
For numbers divisible by 5, print out "Buzz".
For numbers divisible by both 3 and 5, print out "FizzBuzz" in the console.
Otherwise, just print out the number.

Hello,
"Oops, try again. You printed Fizz when you should have printed FizzBuzz"

Thanks.

``````for (var i=1 ; i< 21 ; i ++ ){

if (i % 3 === 0 ){
console.log("Fizz") }
else if (i % 5 === 0 ){
console.log("Buzz") }
else if (i % 15 === 0 ){
console.log("FizzBuzz") }
else { console.log(i) }

}``````

3. ... And the good!
...And the good! 3/33 Need Help
#2

``````for (var i=1 ; i< 21 ; i ++ ){

if (i % 3 === 0 ){
console.log("Fizz") }
else if (i % 5 === 0 ){
console.log("Buzz") }
else if (i % 15 === 0 ){
console.log("FizzBuzz") }
else { console.log(i) }

}``````

This is called incorrect ordering of conditions and it leads to logical errors.
Lets put numbers and test the code..

lest say 15..

as 3 divides the 15 ,it will be printed "Fizz"

lets say another number 30...

as 3 divides 30,It will be printed "Fizz"

lets say 50...

as 5 divides 50 so it will return... "Buzz"

You can see whatever number comes which is totally divided by 15 are already divided by 3 and 5.
Thus for any numbers that can completely divided by 15, will be divided by 3 or 5. thus their messages will be printed but "FizzBuzz" will never get printed.

Can you change order of this part of code

``````else if (i % 15 === 0 ){
console.log("FizzBuzz") }``````

So we able to get "FizzBuzz" ?

#3

Thank you for your detailed and clear explanation !!

#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.