Skip to content

Instantly share code, notes, and snippets.

@KyleMit KyleMit/12-days.js
Last active Feb 18, 2020

Embed
What would you like to do?
12 Days Challenge
let gifts = ["partridge in a pear tree", "turtle doves", "French hens", "calling birds", "gold rings", "geese a-laying", "swans a-swimming", "maids a-milking", "ladies dancing", "lords a-leaping", "pipers piping", "drummers drumming"]
let numLookup = [
{ cardinal: "a", ordinal: "first" },
{ cardinal: "two", ordinal: "second" },
{ cardinal: "three", ordinal: "third" },
{ cardinal: "four", ordinal: "fourth" },
{ cardinal: "five", ordinal: "fifth" },
{ cardinal: "six", ordinal: "sixth" },
{ cardinal: "seven", ordinal: "seventh" },
{ cardinal: "eight", ordinal: "eigth" },
{ cardinal: "nine", ordinal: "ninth" },
{ cardinal: "ten", ordinal: "tenth" },
{ cardinal: "eleven", ordinal: "eleventh"},
{ cardinal: "twelve", ordinal: "twelfth" }
]
let dailyGift = (day) => {
let count = numLookup[day].cardinal
let gift = gifts[day]
return `${count} ${gift}`
}
let toProperCase = s => s[0].toUpperCase() + s.substr(1)
let verses = gifts.map((el, i) => {
let numText = numLookup[i].ordinal
let chorus = `On the ${numText} day of Christmas\nMy true love gave to me`
let cumDays = [...Array(i+1).keys()]
let cumGift = cumDays.map(dailyGift)
if (i>0) {cumGift[0] = `and ${cumGift[0]}`}
let lines = cumGift.map(toProperCase).reverse().join('\n')
let verse = `${chorus}\n${lines}.`
return verse
})
let song = verses.join('\n\n')
console.log(song)
let gifts=["partridge in a pear tree","turtle doves","French hens","calling birds","gold rings","geese a-laying","swans a-swimming","maids a-milking","ladies dancing","lords a-leaping","pipers piping","drummers drumming"],
nums=[{c:"a",o:"first"},{c:"two",o:"second"},{c:"three",o:"third"},{c:"four",o:"fourth"},{c:"five",o:"fifth"},{c:"six",o:"sixth"},{c:"seven",o:"seventh"},{c:"eight",o:"eighth"},{c:"nine",o:"ninth"},{c:"ten",o:"tenth"},{c:"eleven",o:"eleventh"},{c:"twelve",o:"twelfth"}],
song=gifts.map((e,i)=>{
let o=`On the ${nums[i].o} day of Christmas\nMy true love gave to me`
let c=[...Array(i+1).keys()].map(e=>`${nums[e].c} ${gifts[e]}`)
i>0&&(c[0]=`and ${c[0]}`)
let l=c.map(e=>e[0].toUpperCase()+e.substr(1)).reverse().join("\n");
return `${o}\n${l}.`
}).join("\n\n");
console.log(song);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.