Skip to content

Instantly share code, notes, and snippets.

@panzerstadt
Created February 8, 2020 08:04
Show Gist options
  • Save panzerstadt/e8f0e49953352fbb7ee93a2e778e817a to your computer and use it in GitHub Desktop.
Save panzerstadt/e8f0e49953352fbb7ee93a2e778e817a to your computer and use it in GitHub Desktop.
generate the song "Twelve days of Christmas"
const gifts = {
first: "a Partridge in a Pear Tree.",
second: "two Turtle Doves",
third: "three French Hens",
fourth: "four Calling Birds",
fifth: "five Gold Rings",
sixth: "six Geese-a-Laying",
seventh: "seven Swans-a-Swimming",
eighth: "eight Mails-a-Milking",
ninth: "nine Ladies Dancing",
tenth: "ten Lords-a-Leaping",
eleventh: "eleven Pipers Piping",
twelfth: "twelve Drummers Drumming",
and: "and"
};
const dayMap = {
1: "first",
2: "second",
3: "third",
4: "fourth",
5: "fifth",
6: "sixth",
7: "seventh",
8: "eighth",
9: "ninth",
10: "tenth",
11: "eleventh",
12: "twelfth",
and: "and"
};
const days = 12;
const buildSentence = day => {
const generateGifts = day => {
const daysArray = Array.from(Array(day)).map((_, i) => i + 1);
const sequence =
day === 1
? daysArray
: [...daysArray.slice(0, 1), "and", ...daysArray.slice(1)];
const suffix = day => (day !== 1 && day !== "and" ? "," : "");
return sequence
.reverse()
.map(dayOfGift => gifts[dayMap[dayOfGift]] + suffix(dayOfGift))
.join(" ");
};
return (
"On the " +
dayMap[day] +
" day of Christmas my true love gave to me: " +
generateGifts(day)
);
};
const recite = days => {
const daysArray = Array.from(Array(days)).map((_, i) => i + 1);
const song = daysArray.map(day => buildSentence(day));
console.log(song.join("\n\n"));
};
recite(days);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment