FizzBuzz question


#1

So I'm just wondering if anyone else sees something I don't that will make this code not function properly.

for (var count = 1; count < 21; count++) {
if (count % 3 === 0) {
console.log("Fizz");
} else if (count % 5 === 0) {
console.log("Buzz");
} else if (count % 3 === 0 && count % 5 === 0) {
console.log("FizzBuzz");
} else {
console.log(count);
}
}


#2

Looks fine for me. Works too. Try refreshing your browser or using another browser for this exercise.


#3

@terarunner93185
I think you should put
else if (count % 3 === 0 && count % 5 === 0) {
console.log("FizzBuzz");
}

before the if statement
if (count % 3 === 0) {
console.log("Fizz");
}

so if the two conditions match, it will directly execute the instructions for Fizzbuzz. If not, JavaScript is going to look at other condition to match. Your code should look like this;

for (var count = 1; count < 21; count++) {
if (count % 3 === 0 && count % 5 === 0) {
console.log("Fizz");
} else if (count % 5 === 0) {
console.log("Buzz");
} else if (count % 3 === 0) {
console.log("FizzBuzz");
} else {
console.log(count);
}
}


#4

It doesn't matter which way the are ordered.


#5

@jibblyj It does,

this is first so if it's divisible by 3 it ill print fizz and move on, it won't bother to check if its also divisible by 5. there for having

last, means it will only check numbers that aren't divisible by 3 or 5 making it useless anyway because it's already been attempted to be divided by 3 and 5