DEV Community

0xkoji
0xkoji

Posted on

2

JavaScript Implementation of FizzBuzz in functional programming

The FizzBuzz problem is a classic test given in coding interviews. The task is simple: Print integers 1 to N, but print “Fizz” if an integer is divisible by 3, “Buzz” if an integer is divisible by 5, and “FizzBuzz” if an integer is divisible by both 3 and 5.

const isFizz = number => number%3 ==0;
const isBuzz = number => number%5 ==0;

const range = (start, end) => [...new Array(end - start).keys()].map((n) => n + start);

const doFizzBuzz = (start, end) => range(start, end).map((number => {
  if(isFizz(number) && isBuzz(number)) {
    return 'FizzBuzz';
  } else if(isFizz(number)) {
    return 'Fizz';
  } else if(isBuzz(number)) {
    return 'Buzz';
  } else {
    return number;
  }
 }))
 .join(`\n`);

console.log(doFizzBuzz(1, 101));
Enter fullscreen mode Exit fullscreen mode

jsfiddle

https://jsfiddle.net/381g4fct/7/

Top comments (0)

nextjs tutorial video

Youtube Tutorial Series

So you built a Next.js app, but you need a clear view of the entire operation flow to be able to identify performance bottlenecks before you launch. But how do you get started? Get the essentials on tracing for Next.js from @nikolovlazar in this video series 👀

Watch the Youtube series

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay