Why Interviewers Still ask ‘FizzBuzz’ in 2022
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!
Related Posts
The Great JavaScript Debate: To Semicolon or Not?
Since I’ve started learning this language, JavaScript has undergone some heavy changes. Most notably, it seems to be the norm to not use semicolons anymore.
Read moreHacktoberfest 2024: Get a Free JavaScript Today Sticker
October is here, and that means one thing in the tech world—Hacktoberfest! This annual event, powered by DigitalOcean, Cloudflare, Quira, and other sponsors, encourages developers of all skill levels to contribute to open-source projects.
Read moreCreating a Real Time Chat Application with React, Node, and TailwindCSS
In this tutorial, we will show you how to build a real-time chat application using React and Vite,as well as a simple Node backend.
Read more