DEV Community

Cover image for Solving the FizzBuzz Interview Question with JavaScript
Rithik Samanthula
Rithik Samanthula

Posted on β€’ Edited on

6 3

Solving the FizzBuzz Interview Question with JavaScript

Hey There!

When I was a kid everyone used to play a game called FizzBuzz. I personally loved playing it with my family members.

Anyways, the game goes like this:

1, 2, fizz, 4, Buzz, fizz, 7, 8, fizz, Buzz

For every multiple of 3. you have to replace it with Fizz.

For every multiple of 5, you have to replace it with Buzz.

Now, during web development interviews, the most common question that interviewers ask are: "Write a program in JavaScript that prints the order of FizzBuzz"

Alt Text

As you can see in this donut chart, 90% of the interviewees fail and 10% pass to do so.

Don't want to be part of that 90%?

Today, we will be learning how to write a program that prints FizzBuzz in JavaScript.

First, create a variable called output, and set it to an empty array:

var output = [];
Enter fullscreen mode Exit fullscreen mode

Then, create a function called fizzBuzz and create a for if condition wrapped around a for loop:

var output = [];


function fizzBuzz() {

 for() {

if () {

}
Enter fullscreen mode Exit fullscreen mode

Then type this in the for and if commands:

function fizzBuzz() {

 for(var count = 1; count < 101; count++) {

if (count % 3 === 0 && count % 5 === 0) {
  output.push("FizzBuzz");
}
Enter fullscreen mode Exit fullscreen mode

After that, use else if statements. Like this:

else if (count % 3 === 0) {
   output.push("Fizz");
} 

else if (count % 5 === 0) {
    output.push("Buzz")
}

else {
   output.push(count);
}
Enter fullscreen mode Exit fullscreen mode

These else and else if should still be in the fizzbuzz function.

Finally, console log the output by using:

console.log(output);

}
Enter fullscreen mode Exit fullscreen mode

This is how the final code should look like:

Test the output by running the JS Code in the console.

To run the code, paste the code and hit enter. Then, use fizzBuzz();

If you get an output like this, then HOORAY! It works.

Alt Text

Now, you've learned how to solve the FizzBuzz challenge and you will not be part of the 90% anymore!

Bonus

Here is an alternate and easier way to solve the FizzBuzz challenge:

Alt Text

Thanks for reading and remember...

Keep Coding Y'All πŸ‘¨πŸ»β€πŸ’»

SurveyJS custom survey software

Build Your Own Forms without Manual Coding

SurveyJS UI libraries let you build a JSON-based form management system that integrates with any backend, giving you full control over your data with no user limits. Includes support for custom question types, skip logic, an integrated CSS editor, PDF export, real-time analytics, and more.

Learn more

Top comments (3)

Collapse
 
jonrandy profile image
Jon Randy πŸŽ–οΈ β€’ β€’ Edited

Here are some more (I omitted the console logging though, these are just the basic solving function):

const fizzbuzz = x=>[x,f='fizz',b='buzz',f+b][!(x%3)|!(x%5)<<1]
Enter fullscreen mode Exit fullscreen mode

and:

const fizzBuzz = x=>({1:x,6:f="fizz",10:b="buzz",0:f+b}[x**4%15])
Enter fullscreen mode Exit fullscreen mode
Collapse
 
therickedge profile image
Rithik Samanthula β€’

Yeah, that's awesome

Collapse
 
dannyengelman profile image
Danny Engelman β€’

But unreadable and not easy to extend

let FB = Array(100)
  .fill((x, div, label) => x % div ? "" : label)
  .map((func, idx) =>
    func(++idx, 3, "Fizz") + func(idx, 5, "Buzz") || idx
  );
Enter fullscreen mode Exit fullscreen mode


`

nextjs tutorial video

Youtube Tutorial Series

So you built a Next.js app, but you need a clear view of the entire operation flow to be able to identify performance bottlenecks before you launch. But how do you get started? Get the essentials on tracing for Next.js from @nikolovlazar in this video series πŸ‘€

Watch the Youtube series

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay