How to Solve the FizzBuzz Code Challenge

by Rob Johnson in on 6 Oct 2014
Last updated: 7 Oct 2014

tl;dr:How to solve a common programming question often brought up during the interview process.

"Write a program that prints the numbers from 1 to 100. But for multiples of three print 'Fizz' instead of the number and for the multiples of five print 'Buzz'. For numbers which are multiples of both three and five print 'FizzBuzz'."

<script>
  //Start the count from 1. Limit to 100.
  for (var  i = 1; i <= 100;) {
    //Define write conditions based off divisibility
    document.write((i % 3 ? "" : "Fizz") + (i % 5 ? "" : "Buzz") || i)
    //Add line breaks for display purposes
    + document.write("<br>");
    //Increment i for next loop
    i++;
  }
</script>

Resources

  1. Example of above code
  2. c2.com/cgi/wiki?FizzBuzzTest
  3. The FizzBuzz Kata, using Ruby

code

Was this article helpful to you? /