Skip to content

Instantly share code, notes, and snippets.

@jjhamshaw
Created April 4, 2011 16:20
Show Gist options
  • Save jjhamshaw/901902 to your computer and use it in GitHub Desktop.
Save jjhamshaw/901902 to your computer and use it in GitHub Desktop.
FizzBuzz Quiz: 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 “
# First version
def fizzbuzz num
answer = []
answer.push 'fizz' if num % 3 == 0
answer.push 'buzz' if num % 5 == 0
answer.push num if num % 3 != 0 and num % 5 != 0
puts answer.join
end
(1..100).each {|x| fizzbuzz x }
# Second version
def is_fizz? num
num % 3 == 0
end
def is_buzz? num
num % 5 == 0
end
def fizzbuzz num
if is_fizz? num and is_buzz? num
'fizzbuzz'
elsif is_fizz? num
'fizz'
elsif is_buzz? num
'buzz'
else
num
end
end
(1..100).each {|x| puts fizzbuzz2 x }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment