I would have started by looking over more than just the question in the linked question. You should have looked at the answers.
First of all, while the added where
clause option in switch
statements is a nice addition to the language, it doesn't make anything better here.
Moreover, we're failing on the single-responsibility-principle by not extracting out a method which takes an Int
and returns a String
and then simply looping over calls to this function.
So, the answer to this question is effectively going to be an a rewrite of the accepted answer of the linked question:
func fizzBuzzify(value: Int) -> String {
switch (value % 3, value % 5) {
case (0,0): return "FizzBuzz"
case (0,_): return "Fizz"
case (_,0): return "Buzz"
default: return String(value)
}
}
func fizzBuzz(startingValue: Int = 1, endingValue: Int = 100) {
for i in startingValue...endingValue {
print(fizzBuzzify(i))
}
}
Then you get it all done in a single line:
fizzBuzz()