Not able to print Fizz for numbers that're divisible by 3,5


#1

var i;
for(i=1;i<=20;i++){

if(i%3===0)
{console.log("Fizz");
}
else if(i%5===0){
console.log(i);
}
else{console.log("FizzBuzz");
}

}


#2

you should use a and operator (&&) to check if the number is divisble by 3 and 5, and it should be your if condition (the first condition) then two else if to check if the number is dividible by 3 or 5 and then else to print the number (i) if none of the conditions are true


#3

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


#4

you have a question? I only see code

well if the number is divisible by 3 and 5 it also divisible by 3 or 5, so if you have that i is 15, the if statement would be true, it never never gets to your fizzbuzz, to check if the number is divisble by 3 and 5 should come first (it should be your if statement)


#5

thank you so much..thank you...=))..just code for fun


#6

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