Skip to content

Instantly share code, notes, and snippets.

@jasonknight
Last active July 29, 2019 14:48
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 jasonknight/dc0d415176ca8e96c309940c35cdcb8d to your computer and use it in GitHub Desktop.
Save jasonknight/dc0d415176ca8e96c309940c35cdcb8d to your computer and use it in GitHub Desktop.
Trilogy Challenge: FooBar (FizzBuzz)
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