Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Why is this code not running properly? Here are the rules:
Print out the numbers from 1 - 20.
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.
Otherwise, just print out the number.
Here is my code:
C#
for(var i = 1;i < 21; 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%3===0 && i&5===0) {
        console.log("FizzBuzz");
    } else {
        console.log(i);
    }
};

Thank you for your time, and if you find the problem in this code, explain to me why it is wrong and how to fix it. Thank you!
Posted
Comments
UWStudents 23-Feb-15 18:49pm    
Ohhh, I've got it, you have to do the "FizzBuzz" statement first! I still don't understand why you have to do it that way.
PIEBALDconsult 23-Feb-15 19:34pm    
https://www.youtube.com/watch?v=fLrpBLDWyCI
Mohibur Rashid 23-Feb-15 20:39pm    
example number : 15
it is divisible by both 3 and 5. if you do by 3 it will execute Fizz and skip rest.
And your if statement would be much more simpler.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900