I know how to write simple FizzBuzz code in JavaScript:
x = 0;while (++x < 1000)console.log((x % 3 ? "" : "Fizz") + (x % 5 ? "" : "Buzz") || x);
I know how to write simple FizzBuzz code in JavaScript:
x = 0;while (++x < 1000)console.log((x % 3 ? "" : "Fizz") + (x % 5 ? "" : "Buzz") || x);
But how can we get the same result without using the '%' operator?
Share Improve this question edited Feb 6, 2018 at 3:23 Peter Mortensen 31.6k22 gold badges110 silver badges133 bronze badges asked Jan 14, 2018 at 5:21 Dimpu Aravind BuddhaDimpu Aravind Buddha 9,8454 gold badges19 silver badges23 bronze badges 1- @NisargShah Thanks. I updated it. It's was an interview question. – Dimpu Aravind Buddha Commented Jan 15, 2018 at 21:13
7 Answers
Reset to default 5Think what %
actually is, it is just the remainder when you divide a number by something. You can divide the number, then round the result down, then multiply it with the divisor and subtract from the number.
const mod = (num,div)=> {
const res = (num / div) | 0; // coerce to int
return num - (res * div);
}
console.log(mod(8,5));
You an use Number.isInteger
.
const oneToOneHundred = Array.from({ length: 100 }, (_, i) => i + 1);
const fizzBuzz = (i) => {
if (Number.isInteger(i / 15)) return 'FizzBuzz';
if (Number.isInteger(i / 3)) return 'Fizz';
if (Number.isInteger(i / 5)) return 'Buzz';
return i;
};
console.log(oneToOneHundred.map((i) => fizzBuzz(i)).join('\n'));
This is the alternative way by subtracting the number until it get negative value, then add with the divider to get the remainder.
function mod(number, divider){
var num = number;
while(num>=0){
num = num - divider;
}
num = num + divider;
return num;
}
console.log(mod(92, 3) == 92%3);
Here's a simple solution of fizzbuzz without modulo, and without writing your own modulo implementation. It relies on simple counters.
var i=0, n=0, f=1, b=1;
while(i++<156)
{
n=i;
if(f++==3)
{
document.write('Fizz');
f=1; n='';
}
if(b++==5)
{
document.write('Buzz');
b=1; n='';
}
document.write(n+'<br/>');
}
We can check if the number is divisble by 3 or 5 without needing a modulo operation, or even a division operator.
If we sum all the digits, and it's either 3, 6, or 9, then the number is divisible by 3
If we check the last digit of a number, and it's either 0 or 5, then it's divisible by 5.
Code looks like so:
function isDivisibleByThree(i) {
let sum = getDigits(i).reduce((sum, digit) => sum + digit);
return sum > 9 ? isDivisibleByThree(sum) : (sum === 3 || sum === 6 || sum === 9);
}
function isDivisibleByFive(i) {
let lastDigit = getDigits(i).pop();
return lastDigit === 5 || lastDigit === 0
}
function getDigits(i) {
return Array.from(i.toString()).map(Number);
}
for (let i = 1; i <= 100; i++) {
let val = "";
if (isDivisibleByThree(i))
val += "fizz";
if (isDivisibleByFive(i))
val += "buzz";
console.log(val ? val : i);
}
The Modulus operator %
is simply the remainder of two numbers, you can create your own function that returns the remainder and replace it with your operator.
x = 0;
while (++x < 1000) {
console.log((modulo(x, 3) ? "" : "Fizz") + (modulo(x, 5) ? "" : "Buzz") || x);
}
function modulo(num1, num2) {
if (num2 === 0 || isNaN(num1) || isNaN(num2)) {
console.log("NaN");
return NaN;
} else if (num2 == 1) {
//x mod 1 always = 0
return 0;
}
num1 = Math.abs(num1);
num2 = Math.abs(num2);
return num1 - (num2 * Math.floor((num1 / num2)));
}
Thanks everyone for your answers. I found solved it in two ways one using the hash table and by using recursion. I prefer recursion so.
function fuzzBuzz(fuzz_count,buzz_count,fuzz_buzz_count,counter) {
if(fuzz_buzz_count === 15) {
console.log("fuzzbuzz");
fuzz_buzz_count = 0;
buzz_count = 0;
fuzz_count = 0;
} else if(buzz_count === 5) {
console.log("buzz");
buzz_count = 0;
} else if (fuzz_count === 3) {
console.log("fuzz");
fuzz_count = 0;
} else {
console.log(counter);
}
if(counter < 100) {
fuzzBuzz(++fuzz_count, ++buzz_count, ++fuzz_buzz_count, ++counter);
}
}
fuzzBuzz(1,1,1,1);