FizzBuzz help!


#1


No sure what i'm doing wrong .... can anyone help???


for (var i = 0; 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

here missed a ;

for (var i = 0; i < 21 i = ++) { //
and
write i++ instead of i=++
so your for loop should be like
for (var i = 0; i < 21; i++) {

and here
else if (i % 3 === 0) && (i % 5 === 0)
should be inside bracket

else if ((i % 3 === 0) && (i % 5 === 0)) {

do you how if else work?
if first condition false then it'll go to second conditon if it's too false then it'll go third condition condition and so on.... at last else .


#3

Thanks, I get a little confused with if/else statements.

I changed my code to the corrections you gave me but when I go to submit it. I get this message

"Oops, try again. It looks like you printed out the wrong number of items."


#4

Instructions:

Print out the numbers from 1 - 20.

for(var i = 0;i<=21;i++) // so you should take i=1

next

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.

how if else work:

if first condition is false
then it'll check second condition
if it's too false
then it'll check third condition
if it's too false
then it'll print else statement.
but if first condition is true
then it'll not go in second condition.

so when i=15

It'll check

if((i % 3) === 0)

that is true.so it'll print "Fizz".did you really want that?

make sense :slight_smile:


#5

I'm confused, The rules aren't clear.

:confused:


#7

Thank you! Finally works! :slight_smile:


#8

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

The right answer to this exercise is trying to evaluate "fizzbuzz" inside "fizz" and "buzz" and remember that "i" variable from for loop should begin at 1, not at 0, otherwise when you submit you are going to get "Oops, try again. You printed FizzBuzz when you should have printed 1" message


#9

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