A programming kata is an exercise which helps a programmer hone his skills through practice and repetition.
This article is part of the series "Scala Tutorial Through Katas". Articles are divided into easy, medium and hard. Beginners should start with easy ones and move towards more complicated once they feel more comfortable programming in Scala.
For the complete list of Scala katas and solutions please visit the index page
Tests that prove that the solution is correct are displayed below. Recommended way to solve this kata is to write the implementation for the first test, confirm that it passes and move to the next. Once all of the tests pass, the kata can be considered solved.
One possible solution is provided below the tests. Try to solve the kata by yourself first.
Fizz Buzz Kata
Return "fizz", "buzz" or "fizzbuzz".
For a given natural number greater than zero return:
- "fizz" if the number is dividable by 3
- "buzz" if the number is dividable by 5
- "fizzbuzz" if the number is dividable by 15
- the same number if no other requirement is fulfilled
Test code can be found in the GitHub FizzBuzz.scala.
[ONE POSSIBLE SOLUTION]
Better solution was provided by rayyildiz.
[BETTER POSSIBLE SOLUTION]
What was your solution? Post it as a comment so that we can compare different ways to solve this kata.