FizzBuzz is a group word game for children to teach them about division. Players take turns to count, replacing any number divisible by three with the word “fizz”, and any number divisible by five with the word “buzz”.

Adults may play FizzBuzz as a drinking game, where making a mistake leads to the player having to make a drinking-related forfeit.

Here’s a slightly different FizzBuzz function:

function fizzbuzz(n) {
    for (var i = 1; i <= n; i++) 
        console.log([i, "Fizz", "Buzz", "FizzBuzz"][(i%3==0) + 2*(i%5==0)]);
}

This method can be easily expanded to work with at any size, e.g. 3, 5 & 7:

function fizzbuzz(n) {
    for (var i = 1; i <= n; i++) {
        var text = [i, "Fizz", "Buzz", "FizzBuzz", "Bang", "FizzBang", "BuzzBang", "FizzBuzzBang"];
        console.log(text[(i%3==0) +  2*(i%5==0) + 4*(i%7==0)]);
}

Output:

Tagged on: