Last active
May 15, 2018 21:46
-
-
Save wolverineks/93b7b6639aa77c66ed58d732c55ccc06 to your computer and use it in GitHub Desktop.
Fizz Buzz
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const ifType = type => cb => item => typeof item === type ? cb(item) : item | |
const ifNumber = ifType('number') | |
const isDivisibleBy = divisor => num => num % divisor === 0 | |
const isFizz = isDivisibleBy(3) | |
const isBuzz = isDivisibleBy(5) | |
const isBang = isDivisibleBy(15) | |
const convert = predicate => converted => num => predicate(num) ? converted : num | |
const toFizz = convert(isFizz)('fizz') | |
const toBuzz = convert(isBuzz)('buzz') | |
const toBang = convert(isBang)('bang') | |
const log = item => console.log(item) | |
const array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15] | |
( | |
array | |
.map(ifNumber(toBang)) | |
.map(ifNumber(toBuzz)) | |
.map(ifNumber(toFizz)) | |
.forEach(log) | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const isDivisibleBy = bottom => top => top % bottom === 0 | |
const isFizz = isDivisibleBy(03) | |
const isBuzz = isDivisibleBy(05) | |
const isBang = isDivisibleBy(15) | |
const createArray = ({ from = 0, to = 100, accumulator = [] }) => { | |
if (from > to) return accumulator | |
return createArray({ from: from + 1, to: to, accumulator: [...accumulator, from] }) | |
} | |
const result = createArray(1, 100).map(num => { | |
if (isBang(num)) return 'BANG' | |
if (isBuzz(num)) return 'BUZZ' | |
if (isFizz(num)) return 'FIZZ' | |
return num | |
}) | |
console.log(result) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment