Created
September 26, 2013 03:09
-
-
Save gregeng/6709385 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
# 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 | |
# Write a loop that will group the numbers from 1 through 50 | |
# by whether they fizz, buzz, or fizzbuzz - 10pts | |
def fizzbuzz(number) | |
if number %3 == 0 && number %5 == 0 | |
"FizzBuzz" | |
elsif number %3 == 0 | |
"Fizz" | |
elsif number %5 == 0 | |
"Buzz" | |
else | |
number | |
end | |
end | |
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # | |
def group_fizzbuzz(first, last) | |
fizz = [] | |
buzz = [] | |
fizzbuzz = [] | |
therest = [] | |
(first..last).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 | |
therest << number | |
end | |
end | |
puts "These are the 'Fizz' numbers: #{fizz}" | |
puts "These are the 'Buzz' numbers: #{buzz}" | |
puts "These are the 'FizzBuzz' numbers: #{fizzbuzz}" | |
puts "These are regular old numbers: #{therest}" | |
end | |
group_fizzbuzz(1,50) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment