简体   繁体   中英

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);}

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;
    }

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.

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);
}
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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM