This is the second of a series of short posts in which I solve interview questions from my Professional Practices course using Ruby.

The Prompt

An oldy but a goodie. Many of you have done this with me before.

The “Fizz-Buzz test” is an interview question designed to help filter out the 99.5% of programming job candidates who can’t seem to program their way out of a wet paper bag. The text of the programming assignment is as follows:

“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”.”

The Solution

(1..100).each do |number|
  multOfThree = number % 3 == 0
  multOfFive  = number % 5 == 0
  if multOfThree && multOfFive
    puts "FizzBuzz"
  elsif multOfThree
	  puts "Fizz"
  elsif multOfFive
    puts "Buzz"
    puts number