DEV Community

Cover image for FizzBuzz without using the Modulo (%) Operator
Vincent Abesamis
Vincent Abesamis

Posted on

3 3

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');
Enter fullscreen mode Exit fullscreen mode

Let's breakdown the code.

I created a reusable function have 5 parameters,

const fizzBuzz = (arrLength, firstNum, secondNum, fizzWord, buzzWord) => {
  // ...
  // ...
  // ...
};
Enter fullscreen mode Exit fullscreen mode
  • 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);
Enter fullscreen mode Exit fullscreen mode

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.

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

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);
  }
});
Enter fullscreen mode Exit fullscreen mode

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

Image of Bright Data

Access Niche Markets with Ease – Unlock restricted market data with precision.

Get access to hard-to-reach data with our specialized proxy services designed for niche markets.

Access Markets

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

πŸ‘‹ Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay