Skip to content

Instantly share code, notes, and snippets.

@jhixson
Last active December 14, 2015 15:38
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 jhixson/7294fb508ebb2e578906 to your computer and use it in GitHub Desktop.
Save jhixson/7294fb508ebb2e578906 to your computer and use it in GitHub Desktop.
Pretty straightforward twelve days of Christmas implementation in JS with a few ES6 bits.
const gifts = ['partridge in a pear tree', 'turtle doves', 'french hens', 'calling birds', 'GOLDEN RINGS', 'geese-a-laying', 'swans-a-swimming', 'maids-a-milking', 'ladies dancing', 'lords-a-leaping', 'pipers piping', 'drummers drumming'];
const ordinals = ['first','second','third','fourth','fifth','sixth','seventh','eighth','ninth','tenth','eleventh','twelfth'];
const numbersToWords = ['One','Two','Three','Four','FIVE','Six','Seven','Eight','Nine','Ten','Eleven','Twelve'];
function twelveDays() {
for(var i = 0; i < gifts.length; i++) {
console.log(`On the ${ordinals[i]} day of Christmas,\nMy true love gave to me,`);
if(i == 0) {
console.log(`a ${gifts[0]}.\n`);
}
else {
giftForDays(i);
}
}
}
function giftForDays(day) {
if(day == 0) {
console.log(`And a ${gifts[0]}.\n`);
}
else {
console.log(`${numbersToWords[day]} ${gifts[day]},`);
giftForDays(day-1);
}
}
twelveDays();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment