Fizz Buzz


#1

So my code wont pass and it does what its supposed to sans printing the strings from the function up top.

Why does it do this?

thank you.

PS if i go and change it to if else statements what should be FizBuzz prints only 'fiz'

function fizzBuzz(){
    for (i=0; i <= 20; i++) {
        if (i%5 === 0) {
            console.log('buzz');
        }
        if (i%3 === 0) {
            console.log('fizz');
        }
       if (i%3 === 0 && i%5 === 0) {
           console.log('FizzBuzz');
       }
       if (i%3 !=0 && i%5 !=0) {
           console.log(i);
       }
    }
}

fizzBuzz();

#2

We need to use else if to prevent fall through. Only one branch should be in effect on any one iteration. Consider that 3 AND 5 need to be tested for, first, and then 3, then 5 (in any order), then log anything else.


#3

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