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
Finding Free and Discounted Programming Books
As an avid reader, I’m always looking for places to find my next book. If they’re free, even better. Although it’s not always so easy finding them, there are plenty available online.
Read moreGetting Started with Google Cloud
In this article, we’re going to be taking a first look at Google Cloud, a leading player in the world of cloud computing, offers services and tools designed to drive innovation and ease operations.
Read moreThe 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 more