Created
June 9, 2013 21:51
-
-
Save Jaltman429/5745408 to your computer and use it in GitHub Desktop.
fizzbuzz.rb
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
# 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