Created
July 16, 2014 17:45
-
-
Save tjeden/b53a25c5836471a6284e to your computer and use it in GitHub Desktop.
fizz_buzz
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
def fizz_buzz(n) | |
result = "" | |
(1..n).to_a.each do |i| | |
x = "" | |
x += "Fizz" if i % 3 == 0 | |
x += "Buzz" if i % 5 == 0 | |
x = i.to_s if x == "" | |
result += x | |
end | |
result | |
end |
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
require 'minitest/autorun' | |
require './fizz_buzz' | |
class TestFizzBuzz < MiniTest::Test | |
def test_display_numbers_to_n | |
assert_equal "1", fizz_buzz(1) | |
assert("1" == fizz_buzz(1)) | |
assert_equal "12", fizz_buzz(2) | |
end | |
def test_fizz | |
assert_equal "12Fizz", fizz_buzz(3) | |
assert_equal "12Fizz4", fizz_buzz(4) | |
end | |
def test_buzz | |
assert_equal "12Fizz4Buzz", fizz_buzz(5) | |
end | |
def test_fizz_buzz | |
assert_equal "12Fizz4BuzzFizz78FizzBuzz11Fizz1314FizzBuzz", fizz_buzz(15) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment