## DEV Community # FizzBuzz without using the Modulo (%) Operator

FizzBuzz is a classic and simple programming challenge or task. You would usually find it in software developer or technical interviews or in coding challenges.

The task is to write a program that outputs the number from 1 to 100. But for multiples of three it should output "Fizz" instead of the number and for the multiple of five output "Buzz". For number which are multiples of both three and five output "FizzBuzz".

Last month, I found myself doing this problem on an interview for a Full Stack JavaScript developer role. But there is a catch, I am not allowed to use the Modulo operator (%).

Here is my solution:

``````const fizzBuzz = (arrLength, firstNum, secondNum, fizzWord, buzzWord) => {
// Creates an array of numbers from 1 to arrLength
const arr = Array.from({ length: arrLength }, (_, i) => ++i);

const mod = (number, div) =>
!(number / div).toString().split('').includes('.');

arr.forEach((a) => {
if (mod(a, firstNum) && mod(a, secondNum)) {
console.log(fizzWord + buzzWord);
} else if (mod(a, firstNum)) {
console.log(fizzWord);
} else if (mod(a, secondNum)) {
console.log(buzzWord);
} else {
console.log(a);
}
});
};

fizzBuzz(100, 3, 5, 'Dog', 'Cat');
``````

Let's breakdown the code.

I created a reusable function have 5 parameters,

``````const fizzBuzz = (arrLength, firstNum, secondNum, fizzWord, buzzWord) => {
// ...
// ...
// ...
};
``````
• arrLength - value of n or the length of the array in my case
• firstNum - the value of 3 in the classic FizzBuzz
• secondNum - the value of 5 in the classic FizzBuzz
• fizzWord - any word that replaces "Fizz"
• buzzWord - any word that replaces "Buzz"
``````const arr = Array.from({ length: arrLength }, (_, i) => ++i);
``````

I learned this method from one of Wes Bos's hot tips.

Array.from() takes a secondary map argument. Handy for creating and populating arrays of a specific length - Wes Bos

``````const mod = (number, div) => !(number / div).toString().split('').includes('.');
// I should have named this function
// isDivisible or doesNotHaveARemainder
``````

This is my hack for my FizzBuzz task, because I am not allowed to use the Modulo operator (%), I created this function. It checks if number is divisible by div and returns a boolean.
It computes the quotient of number and div, then convert the quotient into a string by using the toString() method, splits or convert the string into an array by using split() method, and finally check if the array contains a "." or decimal point by using includes().

Lastly, we iterate, check and print,

``````arr.forEach((a) => {
if (mod(a, firstNum) && mod(a, secondNum)) {
console.log(fizzWord + buzzWord);
} else if (mod(a, firstNum)) {
console.log(fizzWord);
} else if (mod(a, secondNum)) {
console.log(buzzWord);
} else {
console.log(a);
}
});
``````

I was tempted to use map() method on this one, even though it would do the same job on this case, I opted for forEach() because I don't need to return a new array.

JavaScript is a fun and complex language, and I love it for that.

What I learned from that interview is coding challenges are fun, I really enjoyed doing it.

Hi! Thank you for reading my first ever blog post.
Vince