The Twelve Days of Christmas...

It was like an itch. It just had to be scratched. I wrote a VisualBasic version of the the 12 days of Christmas on the back of a bit of Let it Snow Java code, but I knew there was two little issues with it that couldn't be properly conveyed. I knew JavaScript could hit the nail more squarely.

I present you, below, the JavaScript 12 Days of Christmas... Written in 1209 characters and reproduces the song in 2190 characters. Learned the definition of 'th' or 'nd' after numbers as well in the process.

[c]
<script language="JavaScript">

var MyTrueLoveGaveToMe = new Array(
"a partridge in a Pear Tree ",
"2 Turtle Doves ",
"3 French Hens ",
"4 Calling Birds ",
"5 Golden Rings ",
"6 Geese a Laying ",
"7 Swans a Swimming ",
"8 Maids a Milking ",
"9 Ladies Dancing ",
"10 Lords a Leaping ",
"11 Pipers Piping ",
"12 Drummers Drumming "
);
for (var DayOfChristmas=1; DayOfChristmas<13; DayOfChristmas++)
{
switch (DayOfChristmas)
{
case 1:
var Ordinal="st";
break;
case 2:
var Ordinal="nd";
break;
case 3:
var Ordinal="rd";
break;
default:
var Ordinal="th";
break;
}
document.writeln("On the " + DayOfChristmas + Ordinal + " day of christmas my true love gave to me...<br>");
for (var GiftLoop=DayOfChristmas; GiftLoop>0; GiftLoop--)
{
if(GiftLoop==5)
{
document.writeln("<em><strong>");
}
if((DayOfChristmas>1) && (GiftLoop==1))
{
document.writeln("and " + MyTrueLoveGaveToMe[GiftLoop-1] + "<br>");
} else {
document.writeln(MyTrueLoveGaveToMe[GiftLoop-1] + "<br>");
}
if(GiftLoop==5)
{
document.writeln("</em></strong>");
}
}
document.writeln("<br>");
}
</script>


[/c]

...or you can see the result here...

Comments
Sign in or get an account to comment.