May you please help me debug my code


#1


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


#2

Remove the ! on this line,

Remove the one on this line as well,

And also you can turn this into an else statement by removing the condition (i%3==0&& i%5==0) and writing else instead of else if

else syntax in JS looks like,

else {
    //body
}

#3

Hello, on your for condition instead of i++ you put i+=. Correct it and try again.


#4

your for loop:

for(var i=0; i<21; i+=)

i+=? i plus what? you could use ++ as a shorthand, but if you use += you do have to use a value with which i should be increased

the loop should work for the numbers 1 till 20, not 0 till 20 like your loop.

uhm... if a number is divisible by 3 and 5, it also divisible by 3 or 5, with other words, to check if a number is divisible by both, should be your if condition. otherwise, you will never get fizzbuzz

if a number is not fizz, not buzz or not fizzbuzz, the number itself should be printed, this is not happening in your code


#5

Thank you, but I still can't get FizzBuzz and it's saying it's logging to many item.
for(var i=0; i<21; i+=1){
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{};
}


#6

here:

i&3==0

you decided to use & for a reason i can't understand, rather then the modulo operator.


#7

Thank You for helping me.


#8

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