There is an awesome little programmer test called “FizzBuzz”. The challenge is to write a program that prints out numbers from 1 to 100; except the program must follow these rules:
- Multiples of 3 must be printed “Fizz”
- Multiples of 5 must be printed “Buzz”
- Multiples of 3 and 5, like “15” should be printed “FizzBuzz”
After we finished work last night we were looking through Rebecca Murphiy’s awesome JS-Assessment repo. where we saw the FizzBuzz test. We sat down and hammered out a quick FizzBuzz using JavaScript.
We tried to get the FizzBuzz logic down to as few lines as possible, and here is what we came up with.
[javascript]
function fizzbuzz( n ){
return !(n%5) && !(n%3) ? ‘fizzbuzz’ : n%3 ? n%5 ? n : ‘buzz’ : ‘fizz’ ;
};
for(; i<100;){
console.log( i, fizzbuzz(i) );
}
[/javascript]