Skip to content

Instantly share code, notes, and snippets.

@TylerRockwell
Created September 14, 2015 14:21
Show Gist options
  • Save TylerRockwell/5c62dc3fed808cafa526 to your computer and use it in GitHub Desktop.
Save TylerRockwell/5c62dc3fed808cafa526 to your computer and use it in GitHub Desktop.
FizzBuzz Game
require 'minitest/autorun'
require 'minitest/pride'
# Write a method which returns:
#
# * "Fizz" if the number is divisible by 3
# * "Buzz" if the number is divisible by 5
# * "FizzBuzz" if the number is divisible by 3 and 5
# * Otherwise, return the number itself
#
# Remember that the % operator (modulo) is your friend. It returns a zero if one
# number is divisible by another number. In other words, 4 % 2 == 0.
def fizzbuzz(number)
output = ""
output << "Fizz" if number % 3 == 0
output << "Buzz" if number % 5 == 0
output == "" ? number : output
end
class IfChallenge < MiniTest::Test
def test_one
assert_equal 1, fizzbuzz(1)
end
def test_three
assert_equal "Fizz", fizzbuzz(3)
end
def test_five
assert_equal "Buzz", fizzbuzz(5)
end
def test_eight
assert_equal 8, fizzbuzz(8)
end
def test_ten
assert_equal "Buzz", fizzbuzz(10)
end
def test_fifteen
assert_equal "FizzBuzz", fizzbuzz(15)
end
def test_twenty_three
assert_equal 23, fizzbuzz(23)
end
def test_thirty
assert_equal "FizzBuzz", fizzbuzz(30)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment