Skip to content

Instantly share code, notes, and snippets.

@dmi3y
Last active December 24, 2018 04:09
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 dmi3y/f24979b888c91721b8041ab11d9e61f1 to your computer and use it in GitHub Desktop.
Save dmi3y/f24979b888c91721b8041ab11d9e61f1 to your computer and use it in GitHub Desktop.
12DaysOfChristmas
#!/usr/bin/env node
const sumit = (sum, it) => sum + it
// Christmas is around us!
const christmasPresentsPerDays = (days = 12) => {
const bag = [];
for (let i = 0; i < days; i++) {
bag.push(i + 1 + bag[i - 1] || 1);
}
return bag.reduce(sumit, 0);
}
const christmasPresentsPerDaysRec = (days = 12, day = 1, collect = [0]) => {
if (day > days){
day
collect
return collect.reduce(sumit)
} else {
day
collect
let nsum = collect.reduce(sumit) + day
collect.push(nsum)
let nday = day + 1
return christmasPresentsPerDaysRec(days, nday, collect)
}
}
const argvDays = process.argv.length > 2 ? process.argv.pop() : 12
console.log(christmasPresentsPerDays(argvDays) )
console.log(christmasPresentsPerDaysRec(argvDays))
console.log(`For ${argvDays} days you got ${christmasPresentsPerDays(argvDays)} presents!`)
{"name": "12-days-of-christmas", "version": "0.0.0", "bin": "./12daysOfChristmas.js"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment