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

  1. result = [], i = 1
  2. i ≤ n?
  3. i % 15 == 0?
  4. i % 3 == 0?
  5. i % 5 == 0?
  6. Add "FizzBuzz"
  7. Add "Fizz"
  8. Add "Buzz"
  9. Add i as text
  10. i = i + 1
  11. Return result

← All practice problems

Try AI Flowchart free →