3/33 help!


#1

I've been trying all kind of if else statements but can't figure out why my code won't print out "FizzBuzz"???
Here's my code:

for ( 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)
}

};

please help me

#2

There shouldn't need to be an else I think, just print it out.


#3

putt the FizzBuzz code first..like this

for(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)}

}


#4

Thanks la0105, it worked :smile:
but could you explain to me why it should be written in the beginning of the code and not later?


#5

I agree with la0105 put the FizzBuzz first.
teraninja, the computer reads the code from up to down. If it reads your code, then all the Fizzbuzzes, which are divisible by three, will be assigned "Fizz" because it reads the fizz first. Else if means if the stuff that's LEFT is whatever then it will do that code.


#6

yeah..as edwinisawesome said computer reads code from up to down. :grinning: