Skip to content

Instantly share code, notes, and snippets.

@deatheragetr
Created June 9, 2013 01:39
Show Gist options
  • Save deatheragetr/5737257 to your computer and use it in GitHub Desktop.
Save deatheragetr/5737257 to your computer and use it in GitHub Desktop.
FizzBuzz And Fun
def fizzbuzz
i = 1
while i <= 50
if i % 3 == 0 && i % 5 == 0
puts "FizzBuzz"
elsif i % 3 == 0
puts "Fizz"
elsif i % 5 == 0
puts "Buzz"
end
i += 1
end
end
fizzbuzz
# Write a loop that will group the numbers
# from 1 through 50 by whether the fizz, buzz or fizzbuzz
def fizzbuzz_categorizer
fizz = []
buzz = []
fizzbuzz = []
i = 1
while i <= 50
if i % 3 == 0 && i % 5 == 0
fizzbuzz << i
elsif i % 3 == 0
fizz << i
elsif i % 5 == 0
buzz << i
end
i += 1
end
print "And the fizzes are: "
fizz.each do |i|
print "#{i}, " if i != fizz.last
if fizz.last == i
puts "#{i}"
end
end
print "And the buzzes are: "
buzz.each do |i|
print "#{i}, " if i != buzz.last
if buzz.last == i
puts "#{i}"
end
end
print "And the fizzbuzzes are: "
fizzbuzz.each do |i|
print "#{i}, " if i != fizzbuzz.last
if fizzbuzz.last == i
puts "#{i}"
end
end
end
fizzbuzz_categorizer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment