Last active
February 4, 2018 11:08
-
-
Save pe3/efbcd76dc518c10a2989ec1d2cc85df6 to your computer and use it in GitHub Desktop.
declarative javascript sollution to fizzbuzz
This file contains hidden or 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
// It's more interesting if the numbers are -50 to +50. | |
const range = (x1,x2) => [...Array(x2-x1+1)].map( (_,i)=>x1+i ) | |
const maybeFizz = x => x%3==0 ? 'fizz' : undefined | |
const maybeBuzz = x => x%5==0 ? 'buzz' : undefined | |
const join = (s1,s2) => [s1,s2].join('') | |
const maybeText = x => join(maybeFizz(x),maybeBuzz(x)) | |
const textOrNum = x => maybeText(x) || x | |
const res = range(-50,50).map(textOrNum) | |
console.log(res) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment