Skip to content

Instantly share code, notes, and snippets.

@Bishopc94
Last active August 29, 2015 14:06
Show Gist options
  • Save Bishopc94/915c4f069db48ca571f8 to your computer and use it in GitHub Desktop.
Save Bishopc94/915c4f069db48ca571f8 to your computer and use it in GitHub Desktop.
12 days of Christmas Javascript program
window.onload = onPageLoad();
// begins the program
function onPageLoad(){
print12Days();
}
// function prints the song and calls other functions to return the verse in the song and the written day it is on
function print12Days(){
var dayOfChristmas = 1; // counter for the day the song is on
// loops through the days creating a string of the song and displaying as each verse increments
for(dayOfChristmas;dayOfChristmas <= 12;dayOfChristmas++){
var displayString = "<br /> On the " + getDay(dayOfChristmas) + " day of Christmas, my true love gave to me:<br />"; // initializes the song with the first line that begins every verse also retrieves day in written form
var counter = dayOfChristmas;
while(counter >= 1){
if(counter == 1) {
displayString += firstDayOfChristmas() + "<br />";
} else if(counter == 2) {
displayString += secondDayOfChristmas() + ", " + "<br />" + "and ";
} else if(counter == 4) {
displayString += thirdDayOfChristmas() + ", " + "<br />";
}else if(counter == 5) {
displayString += fourthDayOfChristmas() + ", " + "<br />";
}else if(counter == 6) {
displayString += fifthDayOfChristmas() + ", " + "<br />";
}else if(counter == 7) {
displayString += sixthDayOfChristmas() + ", " + "<br />";
}else if(counter == 8) {
displayString += seventhDayOfChristmas() + ", " + "<br />";
}else if(counter == 9) {
displayString += eighthDayOfChristmas() + ", " + "<br />";
}else if(counter == 10) {
displayString += ninthDayOfChristmas() + ", " + "<br />";
}else if(counter == 11) {
displayString += tenthDayOfChristmas() + ", " + "<br />";
}else if(counter == 12) {
displayString += eleventhDayOfChristmas() + ", " + "<br />";
}
counter--;
}
document.write('<p>');
document.writeln(displayString);
document.write('</p>');
}
}
function firstDayOfChristmas(){
var firstDay = "a Partridge in a Pear Tree";
return firstDay;
}
function secondDayOfChristmas(){
var secondDay = "2 Turtle Doves";
return secondDay;
}
function thirdDayOfChristmas(){
var thirdDay = "3 French Hens";
return thirdDay;
}
function fourthDayOfChristmas(){
var fourthDay = "4 Calling Birds";
return fourthDay;
}
function fifthDayOfChristmas(){
var fifthDay = "5 Golden Rings";
return fifthDay;
}
function sixthDayOfChristmas(){
var sixth = "6 Geese a Laying";
return sixth;
}
function seventhDayOfChristmas(){
var seventhDay = "7 Swans a Swimming";
return seventhDay;
}
function eighthDayOfChristmas(){
var eighthDay = "8 Maids a Milking";
return eighthDay;
}
function ninthDayOfChristmas(){
var ninthDay = "9 Ladies Dancing";
return ninthDay;
}
function tenthDayOfChristmas(){
var tenthDay = "10 Lords a Leaping";
return tenthDay;
}
function eleventhDayOfChristmas(){
var eleventhDay = "11 Pipers Piping";
return eleventhDay;
}
function twelvfthDayOfChristmas(){
var twelvfthDay = "12 Drummers Drumming";
return twelvfthDay;
}
// this function is cool because it uses JavaScript rules to change an int variable to a string
function getDay(number){
switch(number){
case 1:
return "first";
case 2:
return "second";
case 3:
return "third";
case 4:
return "fourth";
case 5:
return "fifth";
case 6:
return "sixth";
case 7:
return "seventh";
case 8:
return "eighth";
case 9:
return "ninth";
case 10:
return "tenth";
case 11:
return "eleventh";
case 12:
return "twelfth";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment