FizzBuzz

Easy~10 min

Given a positive integer n, return an array of strings from 1 to n where:

  • Multiples of 3 are replaced with "Fizz"
  • Multiples of 5 are replaced with "Buzz"
  • Multiples of both 3 and 5 are replaced with "FizzBuzz"
  • All other numbers are converted to their string representation

This is one of the most classic coding interview warm-up problems. It tests your ability to use conditional logic and the modulo operator.

Examples

Example 1
Input: n = 3
Output: ["1", "2", "Fizz"]
Explanation: 1 and 2 are not multiples of 3 or 5. 3 is a multiple of 3, so it becomes "Fizz".
Example 2
Input: n = 5
Output: ["1", "2", "Fizz", "4", "Buzz"]
Explanation: 5 is a multiple of 5, so it becomes "Buzz".
Example 3
Input: n = 15
Output: ["1", "2", "Fizz", "4", "Buzz", "Fizz", "7", "8", "Fizz", "Buzz", "11", "Fizz", "13", "14", "FizzBuzz"]
Explanation: 15 is a multiple of both 3 and 5, so it becomes "FizzBuzz".

Constraints

  • 1 <= n <= 10^4
Code
Ctrl+EnterRun|Ctrl+⇧+EnterSubmit
Output

Run your code to see results

Use Cmd+Enter to run