FizzBuzz
A JavaScript exercise from Scriptdrill. Solve it with a code editor in your browser that runs your function against example and hidden tests, and it is checked the moment you submit.
The task
Return an array with the numbers from 1 to `n` as strings, except that multiples of 3 become `"Fizz"`, multiples of 5 become `"Buzz"` and multiples of both become `"FizzBuzz"`. `n` of 0 returns `[]`.
What this exercise teaches
The order of the checks is the whole exercise: a number divisible by both 3 and 5 also passes each single test, so the combined case has to come first (or the two words have to be built up in one string).
A hint if you get stuck
Loop from 1 to `n` and use the remainder operator `%`. Test for multiples of 15 first, otherwise the check for 3 or 5 catches them before you get there.
Try it now, free, no account needed
Every exercise on Scriptdrill is free to try. Open it on the practice page, solve it, and get instant feedback with a hint. The worked solution opens once you solve it or after two tries.
Open this exerciseMore exercises in Basics
- Total of a listFree
- Items in stockFree
- Full namesFree
- Has every tagFree
- Reverse the wordsFree
- Capitalize each wordFree
- Count the vowelsFree
- Clamp a numberFree
- Range of numbersFree
- Palindrome checkFree
- Anagram checkFree
- Fibonacci numbersFree
- Roman numeralsFree
- Balanced bracketsFree