Convert a number to a string, the contents of which depend on the number's factors.
To run the tests, run the command busted
from within the exercise directory.
For more detailed information about the Lua track, including how to get help if you're having trouble, please visit the exercism.io Lua language page.
A variation on a famous interview question intended to weed out potential candidates. http://jumpstartlab.com
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
local raindrops = require('raindrops')
describe('raindrops', function()
it('the sound for 1 is 1', function()
assert.equal('1', raindrops(1))
end)
it('the sound for 3 is Pling', function()
assert.equal('Pling', raindrops(3))
end)
it('the sound for 5 is Plang', function()
assert.equal('Plang', raindrops(5))
end)
it('the sound for 7 is Plong', function()
assert.equal('Plong', raindrops(7))
end)
it('the sound for 6 is Pling as it has a factor 3', function()
assert.equal('Pling', raindrops(6))
end)
it('2 to the power 3 does not make a raindrop sound as 3 is the exponent not the base', function()
assert.equal('8', raindrops(8))
end)
it('the sound for 9 is Pling as it has a factor 3', function()
assert.equal('Pling', raindrops(9))
end)
it('the sound for 10 is Plang as it has a factor 5', function()
assert.equal('Plang', raindrops(10))
end)
it('the sound for 14 is Plong as it has a factor 7', function()
assert.equal('Plong', raindrops(7))
end)
it('the sound for 15 is PlingPlang as it has a factor 3 and 5', function()
assert.equal('PlingPlang', raindrops(15))
end)
it('the sound for 21 is PlingPlong as it has factors 3 and 7', function()
assert.equal('PlingPlong', raindrops(21))
end)
it('the sound for 25 is Plang as it has a factor 5', function()
assert.equal('Plang', raindrops(25))
end)
it('the sound for 27 is Pling as it has a factor 3', function()
assert.equal('Pling', raindrops(27))
end)
it('the sound for 35 is PlangPlong as it has factors 5 and 7', function()
assert.equal('PlangPlong', raindrops(35))
end)
it('the sound for 49 is Plong as it has a factor 7', function()
assert.equal('Plong', raindrops(49))
end)
it('the sound for 52 is 52', function()
assert.equal('52', raindrops(52))
end)
it('the sound for 105 is PlingPlangPlong as it has factors 3, 5 and 7', function()
assert.equal('PlingPlangPlong', raindrops(105))
end)
it('the sound for 3125 is Plang as it has a factor 5', function()
assert.equal('Plang', raindrops(3125))
end)
end)
return function(number)
local result
result = (number%3 == 0 and 'Pling' or '') .. (number%5 == 0 and 'Plang' or '') .. (number%7 == 0 and 'Plong' or '')
return #result == 0 and tostring(number) or result
end
A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.
Here are some questions to help you reflect on this solution and learn the most from it.
Level up your programming skills with 3,449 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors. Exercism is 100% free forever.
Sign up Learn More
Community comments
parens around the anded components may have helped me out with the intended logic bug I figured it out. Interesting approach!
@schecky I agree that the foo and bar or baz pattern looks a bit weird, but after a while you stop trying to parse out the logic and just recognize the pattern as a rough ternary operator.