user10516592
user10516592

Reputation:

JavaScript code keeps repeating statements. How can I stop this?

I'm creating a 12 days of Christmas javascript program and when I print out the statement it keeps repeating the statement. Can you give me any suggestions on how to fix this and get the program to work correctly?

var day = ["first", "second", "third", "fourth", "fifth", "sixth", 
"seventh", "eighth", "ninth", "tenth", "eleventh", "twelfth"];
var song = "";

for (var x = 0; x <= 13; x++) {
song += "On the " + day[x] + " day of Christmas";
song += " my true love gave to me: ";

if (x == 0) {
    song += "a partridge in a pear tree."
} 
else {
    switch (x) {
        case 12:
            song += ("twelve drummers drumming, ");
        case 11:
            song += ("eleven pipers piping, ");
        case 10:
            song += ("ten lords a-leping, ");
        case 9:
            song += ("nine ladies dancing, ");
        case 8:
            song += ("eight maids a-milking, ");
        case 7:
            song += ("seven swans a-swimming, ");
        case 6:
            song += ("six geese a-laying, ");
        case 5:
            song += ("five gold rings,");
        case 4:
            song += ("four calling birds, ");
        case 3:
            song += ("three french hens, ");
        case 2:
            song += ("two turtle doves ");
        case 1:
            song += ("and a partridge in a pear tree.");
            break;
    }
}
console.log(song);}

Upvotes: 2

Views: 467

Answers (4)

Mobasher Fasihy
Mobasher Fasihy

Reputation: 1061

var day = ["first", "second", "third", "fourth", "fifth", "sixth", 
"seventh", "eighth", "ninth", "tenth", "eleventh", "twelfth"];
var dayMessages = ["a partridge in a pear tree.", "and a partridge in a pear tree.", "two turtle doves ", "three french hens, ", "four calling birds, ", "five gold rings,", "six geese a-laying, ", 
"seven swans a-swimming, ", "eight maids a-milking, ", "ten lords a-leping, ", "ten lords a-leping, ", "eleven pipers piping, ", "twelve drummers drumming, "];
var song = "";

for (var x = 0; x <= 13; x++) {
  song = "On the " + day[x] + " day of Christmas";
  song += " my true love gave to me: ";
  song += dayMessages[x];

  console.log(song);
}

Upvotes: 0

Lorenzo Govender
Lorenzo Govender

Reputation: 1

Your switch statement requires a breaks within the case and also the song variable needs to set to empty at the start of the loop, also your switch cases needs to start at zero so it gets the correct case each time :

for (var x = 0; x < 12; x++) {
    song = "";    
    song += "On the " + day[x] + " day of Christmas";
    song += " my true love gave to me: ";

    if (x == 0) {
        song += "a partridge in a pear tree."
    } 
    else {
        switch (x) {
            case 11:
                song += ("twelve drummers drumming, ");
                break;
            case 10:
                song += ("eleven pipers piping, ");
                break;
            case 9:
                song += ("ten lords a-leping, ");
                break;
            case 8:
                song += ("nine ladies dancing, ");
                break;
            case 7:
                song += ("eight maids a-milking, ");
                break;
            case 6:
                song += ("seven swans a-swimming, ");
                break;
            case 5:
                song += ("six geese a-laying, ");
                break;
            case 4:
                song += ("five gold rings,");
                break;
            case 3:
                song += ("four calling birds, ");
                break;
            case 2:
                song += ("three french hens, ");
                break;
            case 1:
                song += ("two turtle doves ");
                break;
            case 0:
                song += ("and a partridge in a pear tree.");
                break;
            default:
        }
    }
    console.log(song);
}

Upvotes: 0

Rubin bhandari
Rubin bhandari

Reputation: 1951

In your switch statement, you have missed break statement. Also you could place the x==0 case on switch itself , no need to have a separate if statement for that.

Upvotes: 0

Manu Bhardwaj
Manu Bhardwaj

Reputation: 1051

break statement missing in switch cases.

switch (x) {
        case 12:
            song += ("twelve drummers drumming, ");
            break;
        case 11:
            song += ("eleven pipers piping, ");
            break;
        case 10:
            song += ("ten lords a-leping, ");
            break;
        case 9:
            song += ("nine ladies dancing, ");
            break;
        case 8:
            song += ("eight maids a-milking, ");
            break;
        case 7:
            song += ("seven swans a-swimming, ");
            break;
        case 6:
            song += ("six geese a-laying, ");
            break;
        case 5:
            song += ("five gold rings,");
            break;
        case 4:
            song += ("four calling birds, ");
            break;
        case 3:
            song += ("three french hens, ");
            break;
        case 2:
            song += ("two turtle doves ");
            break;
        case 1:
            song += ("and a partridge in a pear tree.");
            break;
    }

Upvotes: 6

Related Questions