Created
October 9, 2012 02:12
-
-
Save adamjonas/3856188 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
# 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 fizz_buzz(number) | |
fizzbuzz = [] | |
buzz = [] | |
fizz = [] | |
while number <= 49 | |
number += 1 | |
if (number % 3 == 0 && number % 5 == 0) | |
fizzbuzz << number | |
puts "FizzBuzz" | |
elsif number % 5 == 0 | |
buzz << number | |
puts "buzz" | |
elsif number % 3 == 0 | |
fizz << number | |
puts "fizz" | |
else | |
puts number | |
end | |
end | |
puts "All the fizzes are #{fizz.join(', ')}" | |
puts "All the buzzes are #{buzz.join(', ')}" | |
puts "All the fizzbuzzes are #{fizzbuzz.join(', ')}" | |
end | |
fizz_buzz(0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment