Created
April 4, 2011 16:20
-
-
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 “
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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