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);
}
I would expect the following code to create an element which contains a div with the text "Hi" in it. The element appears in the inspector, but no text is visible on the screen. When I change the ...
I would expect the following code to create an element which contains a div with the text "Hi" in it. The element appears in the inspector, but no text is visible on the screen. When I change the ...
I have HTML like below <ul id="tabStripSecondaryElements" class="k-tabstrip-items k-reset" data-role="tabstrip" style="margin-left: 37px; margin-right: 40px;" role="tablist" aria-activedescendant="...
I have HTML like below <ul id="tabStripSecondaryElements" class="k-tabstrip-items k-reset" data-role="tabstrip" style="margin-left: 37px; margin-right: 40px;" role="tablist" aria-activedescendant="...
I have the following array: const x = [ {event: "oninput", action: ""}, {event: "onfocus", action: ""} ] Following is the desired output: // event: action { oninput: "", onfocus: "" ...
I have the following array: const x = [ {event: "oninput", action: ""}, {event: "onfocus", action: ""} ] Following is the desired output: // event: action { oninput: "", onfocus: "" ...
I'm currently teaching myself functional programming. I'm trying to translate the following: (define a 3) (define b (+ a 1)) (* (cond ((> a b) a) ((< a b) b) (else -1)) (+...
I'm currently teaching myself functional programming. I'm trying to translate the following: (define a 3) (define b (+ a 1)) (* (cond ((> a b) a) ((< a b) b) (else -1)) (+...