DEV Community

Cover image for FizzBuzz!!!
Raymundo Alva
Raymundo Alva

Posted on

FizzBuzz!!!

Fizz Buzz is a really simple programming exercise used during software development interviews. It is originally a children’s game where players take turns to count incrementally, replacing any number divisible by three with the word “fizz”, and any number divisible by five with the word “buzz”. This is said to be used by interviewers to weed out candidates with insufficient programming ability. This gives the interviewer assurance that you know how to code something basic. Some might say this is a problem that should not have so much thought put into it but I think it is fun to dig into problems and find different solutions. I am going to show the very basic solution and then I will show another way to solve it in a much cleaner way.

The Basic Solution

This solution is the most straight forward. Basically we have to iterate through each number (usually up to 100). If the number is divisible by 3 we print “Fizz” if it is divisible by 5 we print “Buzz”. So let’s start with that. Most people will use a for loop for this but I am going to use a while loop to solve this. Also, I will have this function take an argument so that it can work up to any number you want. This is what my while loop looks like.

function fizzBuzz(num) {  
  let i = 1;

  while (i < num) {  
    if (i % 3 === 0) {  
      console.log('Fizz')  
    }  
    else if (i % 5 === 0) {  
      console.log('Buzz')  
    }  
    else {  
      console.log(i)  
    }  
    i++  
  }  
}
Enter fullscreen mode Exit fullscreen mode

When you run this code you will see something odd. Every number divisible by 15 should be printed as “Fizz” when it should be “FizzBuzz”. For this we need to add a new condition at the beginning of the while loop that checks if a number is divisible by 15 or both 3 and 5.

function fizzBuzz(num) {  
  let i = 1;  
  while (i < num) {  
    if (i % 15 === 0) {  
      console.log('FizzBuzz')  
    }  
    else if (i % 3 === 0) {  
      console.log('Fizz')  
    }  
    else if (i % 5 === 0) {  
       console.log('Buzz')  
    }  
    else {  
      console.log(i)  
    }  
    i++  
  }  
}
Enter fullscreen mode Exit fullscreen mode

Nice! That was simple right? It is not a problem that shows off your best technical skill but it is fun. After solving such a simple problem I naturally thought of different approaches. After messing with ideas I wondered if there could be a one line solution to this problem.

The One-Liner Solution

There were some things to take into consideration when trying to implement a one line solution. I had to find a way to use conditional statements without using multiple lines. I immediately thought about ternary operators to solve this. I was trying to keep my code as clean and expressive as possible so I decided to got with a for loop for this solution. My ternary operators would return an empty string if it didn’t meet the conditions or return the number. This is what my one line solution looks like.

function fizzbuzz(num) {  
  for (let i = 1; i < num; i++) console.log((i % 3 ? '' : 'Fizz') + (i % 5 ? '' : 'Buzz') || i)  
}
Enter fullscreen mode Exit fullscreen mode

Great! I think this solution is short and clean. It may seem kind of silly to think so much about such a simple problem but it is great practice. I am sure there are other solutions out there. If anyone can come up with different solutions leave a comment and share it with everyone. Have fun and happy coding! 😎

Top comments (0)