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!

comments powered by Disqus

Related Posts

Solving a Common Interview Question: the Two Sum Algorithm in JavaScript

Imagine you’re at a lively party, and everyone is carrying a specific number on their back. The host announces a game – find two people whose numbers add up to the magic number, and you win a prize!

Read more

Remove Duplicates from Arrays and Strings in JavaScript

Removing duplicates is a common problem in programming that can arise in various contexts, such as cleaning up data or ensuring unique entries.

Read more

Remote First: 5 Websites for Remote Job Opportunities

Would you prefer to work in an office, or while sitting at a beach somewhere in Thailand (i.e. remotely)? Okay, maybe there’s no beach in this scenario, but there’s definitely silence, and maybe your cat.

Read more