An Interest In:
Web News this Week
- April 1, 2024
- March 31, 2024
- March 30, 2024
- March 29, 2024
- March 28, 2024
- March 27, 2024
- March 26, 2024
July 27, 2021 08:12 am GMT
Original Link: https://dev.to/kojikanao/fizzbuzz-in-functional-programming-with-js-4obn
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(`
`);console.log(doFizzBuzz(1, 101));
jsfiddle
Original Link: https://dev.to/kojikanao/fizzbuzz-in-functional-programming-with-js-4obn
Share this article:
Tweet
View Full Article
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To