Last active
December 23, 2015 22:59
-
-
Save jamesjtong/6706909 to your computer and use it in GitHub Desktop.
fizzbuzz
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
#regular fizzbuzz | |
def fizzbuzz(max) | |
(1..max).each do |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 | |
end | |
fizzbuzz(50) | |
#Write a loop that will group the numbers from 1 through 50 by whether the fizz, buzz or fizzbuzz. | |
def fizzbuzz_grouper(max) | |
fizz=[] | |
buzz=[] | |
fizzbuzz=[] | |
nothing=[] | |
(1..max).each do |number| | |
if number % 3 == 0 && number % 5 == 0 | |
fizzbuzz << number | |
elsif number % 3 == 0 | |
fizz << number | |
elsif number % 5 == 0 | |
buzz << number | |
else | |
nothing << number | |
end | |
end | |
puts "This array is FIZZable #{fizz.inspect}" | |
puts "This array is BUZZable #{buzz.inspect}" | |
puts "This array is FIZZBUZZable #{fizzbuzz.inspect}" | |
puts "This array can't do anything #{nothing.inspect}" | |
end | |
fizzbuzz_grouper(50) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment