Search code examples
javascriptecmascript-6fizzbuzz

How to write FizzBuzz in JavaScript without using % operator


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?


Solution

  • 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);