FIZZBUZZ

Ben Harter-Murphy
3 min readNov 29, 2021

This week for our coding solution we are going to be looking at the FizzBuzz coding challenge. If you have been programming for a while, or you are even new to programming you might have heard of it. It is a very common coding question in the community, and even in some cases, a coding question in interviews. And as always we will be solving this in JavaScript.

Our Problem: We have to write a function that takes in a number. For numbers that are multiples of 3 we want to print out “Fizz”, for numbers that are multiples of 5 we want to print out “Buzz”, for numbers that are both multiples of 3 and 5 we want to print out the entire word “FizzBuzz”. Otherwise we just want to return the number if none of the above conditions are true. Let’s get started!

Setting up our function: I have seen solutions that take in a number or an array of numbers. For the sake of this blog we are going to be focusing on how to dynamically take in any number so that our function works for us at a higher level.

function fizzBuzz(num){}

Here we have our function declaration that takes in a number as an argument. Next we need to set up our conditions so that we can evaluate said number and return the appropriate value.

First Conditional:

I have always been told to start with the most complicated conditional first. So let's do that. We need to return “FizzBuzz” if the number is a multiple of 3 AND 5.

function fizzBuzz(num){  if(num % 3 === 0 && num % 5 === 0){     return "FIZZBUZZ"
}
}

Here we are using the modulo operator along with the double ampersand operator to check both values, and not just one over the other. If you are not familiar with the modulo operator it returns the remainder left over from given number. In our case our numbers are 3 and 5. And we do not want a remainder left over since we want to have our numbers be able to be multiples of 3 and 5. Therefore we want them to both deeply equal or equate to 0.

Second and Third Conditional:

By now you have probably figured out what the next two conditionals will be. Given that we just checked if the number was both a multiple of 3 and 5. Now we only want to return FIZZ if the number is a multiple of 3 and return BUZZ if a number is a multiple of 5. Given that we solved the case for both in one conditional, we are going to want two separate conditionals to handle either conditional.

function fizzBuzz(num){if(num % 3 === 0 && num % 5 === 0){return "FIZZBUZZ"
}else if(num % 3 === 0){
return "FIZZ"
}else if(num % 5 === 0){
return "BUZZ"
}
}

VOILA! If a number is not both a multiple of 3 and 5. It will check the next value!

Final Conditional:

If none of the above equates to true. Meaning that the number is neither a multiple of 3 or 5, or both we want to just return the number itself.

function fizzBuzz(num){if(num % 3 === 0 && num % 5 === 0){return "FIZZBUZZ"
}else if(num % 3 === 0){
return "FIZZ"
}else if(num % 5 === 0){
return "BUZZ"
}else{
return num
}
}

And there you have it, a breakdown of how FIZZBUZZ is evaluated with any given number! I hope this helped and it also made sense. You will see variations of fizzbuzz from time to time. Happy Coding!

--

--