FizzBuzz
Problem statement
Given a whole number n, return a list of n strings, one for each number from 1 to n in order. For each number i:
- If
iis a multiple of both 3 and 5, the string is"FizzBuzz". - Otherwise, if
iis a multiple of 3, the string is"Fizz". - Otherwise, if
iis a multiple of 5, the string is"Buzz". - Otherwise, the string is
iwritten in decimal, such as"7".
Function
fizzBuzz(n: int) → List<String>Examples
Example 1
n = 1return = ["1"]Only the number 1, which is not a multiple of 3 or 5, so it stays "1".
Example 2
n = 10return = ["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz"]3, 6 and 9 are multiples of 3 and become "Fizz". 5 and 10 are multiples of 5 and become "Buzz". The other numbers stay as they are.
Example 3
n = 16return = ["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz","16"]15 is a multiple of both 3 and 5, so it becomes "FizzBuzz", not "Fizz". 16 is neither, so it stays "16".
Constraints
1 <= n <= 1000