FizzBuzz without using the Modulo (%) Operator
Tags: firstpost, javascript, codingchallenge, interviewquestion
Published: 2021-03-11 14:58:43

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:

```javascript
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,

```javascript
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"

```javascript
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
> [Link](https://wesbos.com/tip/array-from-map-function).

```javascript
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()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toString)_ method, splits or convert the string into an array by using _[split()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split)_ method, and finally check if the array contains a "." or _decimal point_ by using _[includes()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes)_.

Lastly, we iterate, check and print,

```javascript
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()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map)_ method on this one, even though it would do the same job on this case, I opted for _[forEach()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/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