JavaScript – FizzBuzz Example

email me

This is my FizzBuzz delight for today. FizzBuzz has been used as an interview screening device for computer programmers for many years. Writing a program to output the first 100 FizzBuzz numbers is a trivial problem for any would-be computer programmer, so interviewers can easily sort out those with insufficient programming ability.

The objective is to print 1-100; when a number is a multiple of three, print Fizz; when a number is a multiple of five, print Buzz; if the number is a multiple of three AND five, print FizzBuzz; if the number doesn’t meet one of those conditions, simply print the number.

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