FizzBuzz
Return a list of the numbers from 1 to n as strings, but use "Fizz" for multiples of 3, "Buzz" for multiples of 5, and "FizzBuzz" for multiples of both.
Write fizzbuzz(n) in Python, fizzBuzz(n) in JavaScript or TypeScript, or void fizzbuzz(int n) in C. Level: Easy.
Examples
Input: n = 5 Output: ['1', '2', 'Fizz', '4', 'Buzz']
Input: n = 15 Output: ['1', '2', 'Fizz', '4', 'Buzz', 'Fizz', '7', '8', 'Fizz', 'Buzz', '11', 'Fizz', '13', '14', 'FizzBuzz']
- 1 ≤ n ≤ 100
Hint
Check "multiple of both" first. If you test 3 before 15, the number 15 stops at "Fizz".
The approach, step by step
- result = [], i = 1
- i ≤ n?
- i % 15 == 0?
- i % 3 == 0?
- i % 5 == 0?
- Add "FizzBuzz"
- Add "Fizz"
- Add "Buzz"
- Add i as text
- i = i + 1
- Return result