4

Fizzbuzz

for (var num = 0; num <= 100; num++)

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

Comments
  • 1
    This is probably performant but not eloquent. Eloquent 'Java'?
  • 0
    Typo :/ eloquentjavascript
  • 1
  • 0
    @Netux thank you
  • 1
    You skipped the curly braces, you monster.
  • 1
    if(num % 15==0)
  • 2
    Console.log(num%15?"FizzBuzz":(num%5?"Buzz":(num%3?"Fizz":num)));
  • 4
    let i = 0;

    while( i++ <= 100 ) {
    let message = "";

    i % 3 || ( message += "Fizz" );
    i % 5 || ( message += "Buzz" );

    console.log( message || i );
    }
  • 0
    You don't need a separate case for when Fizz and Buzz are together! (You probably do if you're using console.log. But there are other output functions that don't append a newline.)
  • 0
    for(var num = 0; num <= 100; num++) {
    var out = "";

    if(num % 3 == 0)
    out += "Fizz";

    if(num % 5 == 0)
    out += "Buzz";

    console.log(out || num);
    }
  • 0
    Why is my indentation not saving?!? People are gonna think I'm a scrub.
Add Comment