Philipp Burckhardt

On Statistics, Programming and the Social Sciences

FizzBuzz

Three different implementations of FizzBuzz, a standard programming exercice. FizzBuzz is a word game in which any number divisible by 3 is replaced by "Fizz", every number divisble by 5 is instead printed as "Buzz" and consequently every number divisble by both 3 and 5 as "FizzBuzz". Open the curtain for the code:

JavaScript

var i;

for ( i = 1; i <= 100; i++ ) {
  if ( i % 15 === 0 )
    console.log( "FizzBuzz" )
  else if ( i % 5 === 0 )
    console.log( "Fizz" )
  else if ( i % 3 === 0 )
    console.log( "Buzz" )
  else 
      console.log( i ) 
}

CoffeeScript

fizzbuzz = (num) ->
  return 'FizzBuzz' if 0 == num %% 15
  return 'Buzz'     if 0 == num %% 5
  return 'Fizz'     if 0 == num %% 3
  return String(num)

[1..100].forEach( (i) =>
   console.log fizzbuzz(i)
)

JavaScript (ES6)

'use strict'
function* fizzbuzz( max ) {
  for ( let i = 1; i <= max; i++ ) {
    if ( i % 15 === 0 )
        yield 'FizzBuzz'
    if ( i % 3 === 0 )
        yield 'Fizz'
    else if ( i % 5 === 0 )
        yield 'Buzz'
    else
        yield String(i)     
  }
}    

for ( let fizz of fizzbuzz(100) )
  console.log(fizz)