Last active
December 23, 2015 23:09
-
-
Save ivanbrennan/6708215 to your computer and use it in GitHub Desktop.
FizzBuzz baby!
This file contains 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
def fizzbuzz(number) | |
if (number % 3 == 0) && (number % 5 == 0) | |
puts "FizzBuzz" | |
elsif number % 3 == 0 | |
puts "Fizz" | |
elsif number % 5 == 0 | |
puts "Buzz" | |
else | |
puts number | |
end | |
end | |
i = 0 | |
fiz_nums = "" | |
buz_nums = "" | |
fzbz_nums = "" | |
reg_nums = "" | |
while i < 50 | |
i+=1 | |
if (i % 3 == 0) && (i % 5 == 0) | |
fzbz_nums += " " + i.to_s | |
elsif i % 3 == 0 | |
fiz_nums += " " + i.to_s | |
elsif i % 5 == 0 | |
buz_nums += " " + i.to_s | |
else | |
reg_nums += " " + i.to_s | |
end | |
end | |
puts "Fizz numbers:" + fiz_nums | |
puts "Buzz numbers:" + buz_nums | |
puts "FizzBuzz numbers:" + fzbz_nums | |
puts "Regular numbers:" + reg_nums | |
# Alternate implementation | |
def fizzbuzz(number) | |
if number % 15 == 0 | |
puts "FizzBuzz" | |
elsif number % 3 == 0 | |
puts "Fizz" | |
elsif number % 5 == 0 | |
puts "Buzz" | |
else | |
puts number | |
end | |
end | |
# Wasn't sure if we were supposed to run our method too, but here goes... | |
(1..100).each {|i| fizzbuzz(i)} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment