FizzBuzz in JavaScript
Beginner · 4 min read
FizzBuzz is probably the most famous small programming exercise. Count from 1 upward, but say "Fizz" for multiples of 3, "Buzz" for multiples of 5, and "FizzBuzz" for numbers that are multiples of both. It is a common interview question, because it is short and still shows whether someone can turn a rule into correct code in the right order.
In this tutorial you will write fizzbuzz(n), which returns the right word (or the number, as text) for one number, and then use it for 1 to 20. You need to know what a function and an if statement are, and how the remainder operator % works: 9 % 3 is 0 because nothing is left over, so 9 is a multiple of 3.
The rule, written out
For each number:
- If it divides by 3 and by 5, say "FizzBuzz" (15, 30, 45…).
- Otherwise, if it divides by 3, say "Fizz" (3, 6, 9…).
- Otherwise, if it divides by 5, say "Buzz" (5, 10, 20…).
- Otherwise, say the number itself.
A number that divides by both 3 and 5 always divides by 15, so "divides by both" can be written n % 15 === 0.
Here is the exercise. Have a go yourself first if you like, then follow along.
19 FizzBuzz, and why it is asked
easyFor the numbers 1 to 20, produce Fizz for multiples of 3, Buzz for multiples of 5, FizzBuzz for multiples of both, and the number otherwise.
Stuck? Three hints, smallest first
- 1. Test for both first. Testing for 3 first means 15 never reaches the both-case.
- 2. Building the word by appending avoids the ordering problem entirely.
- 3. Return an array rather than logging, so it can be asserted.
Step 1: the order trap
The natural way to start is with the 3 case, since that is what you read first:
function buggy(n) {
if (n % 3 === 0) return 'Fizz';
if (n % 15 === 0) return 'FizzBuzz';
return String(n);
}
console.log(buggy(15)); // Fizz
15 should be "FizzBuzz", but it comes out "Fizz". return ends the function, so once the first check matches, the second one is never reached. Every multiple of 15 is also a multiple of 3, so the FizzBuzz line can never run.
Step 2: most specific check first
Put the "both" case at the top, then the single cases.
function fizzbuzzOrdered(n) {
if (n % 15 === 0) return 'FizzBuzz';
if (n % 3 === 0) return 'Fizz';
if (n % 5 === 0) return 'Buzz';
return String(n);
}
console.log(fizzbuzzOrdered(15)); // FizzBuzz
console.log(fizzbuzzOrdered(9)); // Fizz
console.log(fizzbuzzOrdered(7)); // 7
String(n) turns the number into text, so the function always returns the same type, a string. That makes the results easy to compare and to join.
This version is correct. But it only works because the lines are in the right order, and that is easy to break when someone edits it later.
Step 3: build the word instead
Here is a version that has no order to get wrong. Start with an empty string. If the number divides by 3, add "Fizz". If it divides by 5, add "Buzz". For 15, both happen, and you get "FizzBuzz" without a separate check.
function fizzbuzz(n) {
let out = '';
if (n % 3 === 0) out += 'Fizz';
if (n % 5 === 0) out += 'Buzz';
return out || String(n);
}
console.log(fizzbuzz(3)); // Fizz
console.log(fizzbuzz(5)); // Buzz
console.log(fizzbuzz(15)); // FizzBuzz
console.log(fizzbuzz(1)); // 1
out += 'Fizz' adds text to the end of out. The last line uses ||, "or": an empty string counts as false, so if nothing was added, the function returns the number instead.
Step 4: run 1 to 20
Array.from({ length: 20 }, (_, i) => …) builds an array of 20 items. For each one it calls the function with the position i, from 0 to 19. Adding 1 gives the numbers 1 to 20. The _ is a common name for an argument you don't use.
const upTo20 = Array.from({ length: 20 }, (_, i) => fizzbuzz(i + 1));
console.log(upTo20.join(' '));
// 1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz 16 17 Fizz 19 Buzz
Returning an array, not printing inside the loop, means you can check the results as well as show them.
The full solution
The complete answer has the ordered version, the build-the-word version and the buggy one. It prints 1 to 20 and then shows the buggy version getting 15 wrong. A check underneath confirms the two correct versions agree on every number from 1 to 100. Press Run to see it.
// The version where order matters, and the both-case must come first.
function fizzbuzzOrdered(n) {
if (n % 15 === 0) return 'FizzBuzz';
if (n % 3 === 0) return 'Fizz';
if (n % 5 === 0) return 'Buzz';
return String(n);
}
// The version where order cannot go wrong, because the word is built up.
function fizzbuzz(n) {
let out = '';
if (n % 3 === 0) out += 'Fizz';
if (n % 5 === 0) out += 'Buzz';
return out || String(n);
}
const upTo20 = Array.from({ length: 20 }, (_, i) => fizzbuzz(i + 1));
console.log(upTo20.join(' '));
// The classic mistake: checking 3 before 15, so 15 comes out as just Fizz.
function buggy(n) {
if (n % 3 === 0) return 'Fizz';
if (n % 15 === 0) return 'FizzBuzz';
return String(n);
}
console.log('buggy version on 15:', buggy(15));
Output
7 checks that prove this answer
You do not have to read these. They are part of the answer panel above, so pressing
Run there executes all 7 of them and prints ok for each.
That output is the proof, and the build fails if a single check fails.
test('3 is Fizz', fizzbuzz(3), 'Fizz');
test('5 is Buzz', fizzbuzz(5), 'Buzz');
test('15 is FizzBuzz', fizzbuzz(15), 'FizzBuzz');
test('1 is just the number', fizzbuzz(1), '1');
test('the first twenty', upTo20.slice(0, 5), ['1', '2', 'Fizz', '4', 'Buzz']);
test('both versions agree everywhere', Array.from({ length: 100 }, (_, i) => i + 1).every((n) => fizzbuzz(n) === fizzbuzzOrdered(n)), true);
test('the buggy ordering loses FizzBuzz', buggy(15), 'Fizz');
The second version is the better answer and not because it is shorter. Building the word by appending means there is no ordering to get wrong — the 15 case falls out of the other two rather than needing its own branch. Whenever a branch exists only to catch a combination of the other branches, that is usually a sign the logic can be restructured.
Common mistakes
- Checking 3 before 15. Multiples of 15 never reach the FizzBuzz line and come out as "Fizz".
- Using
else ifafter building the word. In Step 3 both checks must run for 15. Withelse, only "Fizz" is added. - Returning a number for some inputs and text for others. Mixing types makes the results harder to compare. Return
String(n).
Try it yourself
- Add a third rule: multiples of 7 say "Bazz". With the Step 3 version, how many lines did you need to add?
- Change the range so the function takes a
max, and returns the list from 1 tomax. - Count how many "Fizz", "Buzz" and "FizzBuzz" there are between 1 and 100.