FizzBuzz

beginner
2,100 views1,540 solved
arraymath

Problem Description

Write a function that returns an array with FizzBuzz results from 1 to n.

Rules:

- For numbers divisible by 3, add "Fizz"

- For numbers divisible by 5, add "Buzz"

- For numbers divisible by both 3 and 5, add "FizzBuzz"

- For all other numbers, add the number as a string

Example:

Input: n = 15

Output: ["1", "2", "Fizz", "4", "Buzz", "Fizz", "7", "8", "Fizz", "Buzz", "11", "Fizz", "13", "14", "FizzBuzz"]

Constraints:

- 1 <= n <= 10^4

JavaScriptPress Ctrl+Enter to run tests
Loading editor...

Test Cases

Test 1

Small input

Input:[ 3 ]
Expected:[ "1", "2", "Fizz" ]
Test 2

Up to first Buzz

Input:[ 5 ]
Expected:[ "1", "2", "Fizz", "4", "Buzz" ]
Test 3

Up to first FizzBuzz

Input:[ 15 ]
Expected:[ "1", "2", "Fizz", "4", "Buzz", "Fizz", "7", "8", "Fizz", "Buzz", "11", "Fizz", "13", "14", "FizzBuzz" ]
ASleekGeek - Beyond the Code. Building the Future.