Skip to main content
Cian Code
  1. Posts/

FizzBuzz

·1 min
Table of Contents

Currency Tables example

Generate currency conversion tables

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 approach can be easily expanded to work with 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)]);
  }
}

Demo #

Output: