Skip to content

Instantly share code, notes, and snippets.

@Jaltman429
Created June 9, 2013 21:51
Show Gist options
  • Save Jaltman429/5745408 to your computer and use it in GitHub Desktop.
Save Jaltman429/5745408 to your computer and use it in GitHub Desktop.
fizzbuzz.rb
# assignment.rb
# FizzBuzz - The Programmer's Stairway to Heaven
# Define the fizzbuzz method to do the following: 10pts
# Use the modulo % method (divisible by)
# 2 % 2 #=> true
# 1 % 2 #=> false
# If a number is divisible by 3, puts "Fizz".
# If a number is divisible by 5, puts "Buzz".
# If a number is divisible by 3 and 5, puts "FizzBuzz"
# Use if statements 2pts
# Use the && operator 3pts
def fizzbuzz (number = gets.chomp.to_i)
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
fizzbuzz
# Write a loop that will group the numbers from 1 through 50
# by whether they fizz, buzz, or fizzbuzz - 10pts
def fizzbuzz ()
number = 1
group_fizzbuzz = []
group_fizz = []
group_buzz = []
while number <= 50
case
when number % 3 == 0 && number % 5 == 0
group_fizzbuzz.push(number)
when number % 3 == 0
group_fizz.push(number)
when number % 5 == 0
group_buzz.push(number)
end
number += 1
end
puts "Here are the fizzbuzz numbers:" + group_fizzbuzz.to_s
puts "Here are the fizz numbers: #{group_fizz}"
puts "Here are the buzz numbers: #{group_buzz}"
end
fizzbuzz()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment