JavaScript代码不断重复语句。我该如何阻止呢?

时间:2019-01-14 06:17:09

标签: javascript

我正在创建一个为期12天的圣诞节javascript程序,当我打印出该语句时,它会不断重复该语句。您能给我任何有关如何解决此问题并使程序正常运行的建议吗?

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

4 个答案:

答案 0 :(得分:4)

在切换案例中缺少break语句。

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

答案 1 :(得分:0)

在switch语句中,您错过了break语句。另外,您可以将x==0大小写放在开关本身上,而无需为此使用单独的if语句。

答案 2 :(得分:0)

您的switch语句需要在大小写之间进行换行,并且在循环开始时还需要将song变量设置为空,而且您的switch case需要从零开始,因此每次都会获取正确的case:

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

答案 3 :(得分:0)

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