Last active
December 24, 2018 04:09
-
-
Save dmi3y/f24979b888c91721b8041ab11d9e61f1 to your computer and use it in GitHub Desktop.
12DaysOfChristmas
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
#!/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!`) |
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
{"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