Skip to content

Instantly share code, notes, and snippets.

@chhhris
Created June 9, 2013 23:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chhhris/5745796 to your computer and use it in GitHub Desktop.
Save chhhris/5745796 to your computer and use it in GitHub Desktop.
# 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 number(x)
if x % 3 == 0 && x % 5 == 0
return 'FizzBuzz'
elsif x % 3 == 0
return 'Fizz'
elsif x % 5 == 0
return 'Buzz'
else
return 'No FizzBuzz here.'
end
end
number(30)
# Write a loop that will group the numbers from 1 through 50
# by whether they fizz, buzz, or fizzbuzz - 10pts
def fizzbuzz(x)
1.upto(x) {
|x|
print x, " ", number(x), "\n"
}
end
fizzbuzz(50)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment