Skip to content

Instantly share code, notes, and snippets.

@pe3
Last active February 4, 2018 11:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pe3/efbcd76dc518c10a2989ec1d2cc85df6 to your computer and use it in GitHub Desktop.
Save pe3/efbcd76dc518c10a2989ec1d2cc85df6 to your computer and use it in GitHub Desktop.
declarative javascript sollution to fizzbuzz
// 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