Last active
July 29, 2019 14:48
-
-
Save jasonknight/dc0d415176ca8e96c309940c35cdcb8d to your computer and use it in GitHub Desktop.
Trilogy Challenge: FooBar (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
tests = [ | |
{ | |
:data => 1, | |
:expect => 1 | |
}, | |
{ | |
:data => 2, | |
:expect => 2 | |
}, | |
{ | |
:data => 3, | |
:expect => "foo" | |
}, | |
{ | |
:data => 6, | |
:expect => "foo" | |
}, | |
{ | |
:data => 5, | |
:expect => "bar" | |
}, | |
{ | |
:data => 10, | |
:expect => "bar" | |
}, | |
{ | |
:data => 15, | |
:expect => "foobar", | |
} | |
] | |
def foo_or_bar(n) | |
return "foobar" if n % 5 == 0 and n % 3 == 0 | |
return "bar" if n % 5 == 0 | |
return "foo" if n % 3 == 0 | |
return n | |
end | |
tests.each do |test| | |
if foo_or_bar(test[:data]) != test[:expect] then | |
puts "#{test[:data]} FAILED" | |
else | |
puts "PASSED" | |
end | |
end | |
while true do | |
print "How many? " | |
iterations = gets.chomp | |
if iterations.downcase == 'q' then | |
exit | |
end | |
(iterations.to_i).times do |i| | |
puts foo_or_bar(i + 1) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment