Skip to content

Instantly share code, notes, and snippets.

@framp
Last active October 31, 2017 11:59
Show Gist options
  • Select an option

  • Save framp/6a9231bd7aeaf6261c00cd0706673b40 to your computer and use it in GitHub Desktop.

Select an option

Save framp/6a9231bd7aeaf6261c00cd0706673b40 to your computer and use it in GitHub Desktop.
Mattia Asti Challenge 2016
const assert = require('assert')
const identity = a => a
const add = a => b => b + a
const call = a => b => b(a)
const multiple = a => b => b%a === 0
const get = a => b => b[a]
const desc = (a, b) => b-a
const makeArray = (start, end) =>
Array.apply(null, {length: end-start+1} ).map((a,i) => start+i)
assert.deepEqual(makeArray(-10, 10), [-10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
const runsHighestDivisor = (divisors, operations) => number => {
const index = divisors.sort(desc).map(multiple).map(call(number)).findIndex(identity)
return (operations[divisors[index]] || add(''))(number)
}
assert.equal(runsHighestDivisor([], {})(1), '1')
const testRunsHighestDivisor = runsHighestDivisor([1,2,3], { 1: add(' Fizz'), 2: add(' Buzz') })
assert.equal(testRunsHighestDivisor(1), '1 Fizz')
assert.equal(testRunsHighestDivisor(2), '2 Buzz')
assert.equal(testRunsHighestDivisor(3), '3')
const fizzBuzz = (start, end) => operations =>
makeArray(start, end)
.map(runsHighestDivisor(Object.keys(operations), operations))
.join('\n')
assert.equal(fizzBuzz(0, 3)({ 1: add(' Fizz') }), ['0 Fizz', '1 Fizz', '2 Fizz', '3 Fizz'].join('\n'))
assert.equal(fizzBuzz(0, 3)({ 3: add(' Fizz'), 2: add(' Buzz') }), ['0 Fizz', '1', '2 Buzz', '3 Fizz'].join('\n'))
console.log(fizzBuzz(0, 100)({
3: add(' Fizz'),
5: add(' Buzz'),
15: add(' FizzBuzz')
}))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment