JavaScript 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);}
Answer:1

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;
    }
Answer:2

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.

Answer:3

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);
}
Answer:4

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="...

  1. find inner element jquery
  2. find inner element javascript
  3. find inner element by id jquery
  4. find element by inner text
  5. find element by inner text selenium
  6. find element by innerhtml
  7. find element by innertext javascript
  8. find element by inner text jquery
  9. selenium find element by innerhtml
  10. selenium find inner element
  11. jquery find inner element by class
  12. js find inner element
  13. css find inner element
  14. jest find inner element
  15. jquery find element by innerhtml
  16. js find element by inner text
  17. xpath find element by inner text
  18. how to find inner element

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: "" ...

  1. convert array object to string
  2. convert array object to array javascript
  3. convert array object to string powershell
  4. convert array object to string javascript
  5. convert array object to list java
  6. convert array object to json
  7. convert array object to array php
  8. convert array object to list
  9. convert array object to list c#
  10. convert array object to string python
  11. convert array object to string c#
  12. convert array object to string php
  13. convert array object to object
  14. convert array object to list python
  15. convert array object to json php
  16. convert array object to object typescript
  17. convert array object to string jquery
  18. convert array object to json jquery
  19. convert array object to array python
  20. convert array object to json in javascript

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)) (+...

  1. translate code from one language to another
  2. translate code from java to python
  3. translate code from matlab to python
  4. translate code from r to python
  5. translate code from welsh to english
  6. translate code from python to c++
  7. translate code from sas to r
  8. translate code from java to c#
  9. translate code from spanish to english
  10. translate code from vb.net to c#
  11. translate code from matlab to r
  12. translate code from matlab to julia
  13. translate code from chinese to english
  14. translate from code to english
  15. translate from code
  16. translate code of conduct
  17. translate code for website
  18. translate code in javascript
  19. translate code in python
  20. translate code in php