This problem has been around for such a long time. Anyone who has been writing code for even a little while has probably come across it. It’s commonly asked as an introductory whiteboard question during interviews. It’s so easy that if you fail it, your interviewer will probably scratch their head wondering why you’ve been invited to interview in the first place.
Everyone that can actually program will be able to solve it. Those that fail this simple problem will most likely not be receiving a job offer. The point of FizzBuzz isn’t to find a great programmer, it’s to filter out candidates by asking an extremely easy question.
However, FizzBuzz is so popular, it’s probably not a good question to ask in the current year. Most beginners have probably read about the problem and know how to solve it. Nonetheless, it is still asked. Other such questions may be asked as well, which you should be able to solve them, if you can solve FizzBuzz.
Let’s explore the problem and take a look at the solution.
A look at FizzBuzz
Directions
- Write a function that prints the numbers from
1 to n
. - For multiples of 3, print
fizz
instead of the number. - For multiples of 5, print
buzz
instead of the number. - For numbers which are multiples of both 3 and 5, print
fizzbuzz
.
Example
fizzBuzz(5);
// 1
// 2
// fizz
// 4
// buzz
Solution
The solution is straightforward. It’s probably the solution every one comes up with after reading the instructions.
function fizzBuzz(n) {
for (let i = 1; i <= n; i++) {
// if the number is a multiple of 3 and 5
if (i % 3 === 0 && i % 5 === 0) {
console.log("fizzbuzz");
}
// else if the number is a multiple of 3
else if (i % 3 === 0) {
console.log("fizz");
}
// else if the number is a multiple of 5
else if (i % 5 === 0) {
console.log("buzz");
}
// else, log the number
else {
console.log(i);
}
}
}
We’re going to use a loop to run through the input, checking if the numbers are multiples of 3 and 5 with the help of the remainder operator. If they are, log fizzbuzz
to the console. Else if a number is a multiple of 3, log fizz
to the console. Else if a number is a multiple of 5, log buzz
. Else, if none of the conditions are met, log the number to the console.
Conclusion
That’s it, folks. The classic FizzBuzz problem solved with JavaScript. Feel free to check out our other articles (e.g. counting vowels, counting steps, checking for palindromes). Have you been asked this question before? Let us know in the comments below.
Happy Coding!
Topics
About the Author

Matt Fay
Matt is the founder of JavaScript Today, a platform dedicated to high-quality JavaScript education and commentary. With a deep curiosity for technology, he gravitates toward understanding how things work and has been diving into the world of information security. Outside of coding, Matt enjoys exploring languages; he can understand some Russian and Italian, and is currently learning Thai.
Discussion (Loading...)
Join the Discussion
Sign in to share your thoughts and engage with the JavaScript Today community.