Fizzbuzz


#1

someone help me out

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%3 === 0 && i%5 === 0 ) {
        console.log("FizzBuzz");
    }
    else {
        console.log(i);
    }
};

#2

well, a number divisible by 3 and 5 is also divisible by 3 or 5 (obviously), so lets say we have 15, lets go by the conditions:

if ( 15%3 === 0 )

which is true, so lets log fizz. Now the condition is met, else if and else won't run. So maybe checking if a number is divisible by 3 and 5 should be your if condition?


#3

how am I supposed to write my code to make the "fizzBuz" to be printed.coz am trying to modify that if part but still aint working


#4

You need to decide what events should happen in order for fizzbuzz to be printed and then compare those steps to what it currently does. Execute your code manually and ask yourself what you're doing wrong.

This bug of yours has nothing to do with javascript, you just have to re-order the events so that they add up to the desired result. You may as well be writing the instructions in English, you may even want to start by writing it in English, testing it manually, and then translating it into javascript.


#6

this worked finally

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


#7

thank you all:relieved:


#8

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