Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Oct 2011
    Posts
    2
    Thanks
    2
    Thanked 0 Times in 0 Posts

    First Post - Basics of switch statement

    Hello all,

    This is my first post here, so I apologize if I didn't format this correctly. I am trying to figure out where I went wrong in my code. The objective is to display all lines of the song using a switch statement. I thought I had my head wrapped around it, but apparently not. Would anyone mind pointing me in the right direction, or telling me what I did wrong here? I would be greatly appreciative! Thank you for your consideration folks.

    Code:
    <html xmlns = "http://www.w3.org/1999/xhtml">
    <head>
    <title>Excercise 8.11</title>
    <script type = "text/javascript">
    for (var day = 1; day <=5; day++) {
    
    switch (day) {
    
    case 1: document.write("On the "+day+"st of Christmas, my true love gave to me, A partridge in a pear tree.");
    
    case 2: doucment.write("On the "+day+"th day of Christmas, my true love gave to me, Two turtle doves,<br>");
    
    case 3: document.write("On the "+day+"th day of Christmas, my true love gave to me, Three French hens,<br>");
    
    case 4: document.write("On the "+day+"th day of Christmas, my true love gave to me, Four calling birds,<br>");
    
    case 5: document.write("On the "+day+"th day of Christmas, my true love gave to me, Five golden rings,<br>");
    break;
    }
    }
    </script>
    </head>
    
    
    <body>
    </body>
    </html>

  • #2
    Regular Coder
    Join Date
    Sep 2011
    Location
    Sweden
    Posts
    154
    Thanks
    1
    Thanked 22 Times in 22 Posts
    You've misspelt document.write once and you didn't grasp the concept that you have to have a break after each case, in order not to continue executing the code in the next case.

    Apart from that your code would work, but using document.write is a horrible practice.

    So it would be better to do:
    Code:
    <html xmlns = "http://www.w3.org/1999/xhtml">
    <head>
    <title>Excercise 8.11</title>
    <script type="text/javascript">
    onload = function(){
      var docWrite = function(text){
        var p = document.createElement('p');
        p.innerHTML = text;
        document.body.appendChild(p);
      };
      for (var day = 1; day <= 5; day++) {
        switch (day) {
          case 1: 
            docWrite("On the "+day+"st day of Christmas, my true love gave to me, A partridge in a pear tree.");
            break
          case 2: 
            docWrite("On the "+day+"nd day of Christmas, my true love gave to me, Two turtle doves.");
            break
          case 3:
            docWrite("On the "+day+"th day of Christmas, my true love gave to me, Three French hens.");
            break
          case 4: 
            docWrite("On the "+day+"th day of Christmas, my true love gave to me, Four calling birds.");
            break
          case 5: 
            docWrite("On the "+day+"th day of Christmas, my true love gave to me, Five golden rings.");
            break
        };  
      };
    };
    </script>
    </head>
    <body>
    </body>
    </html>
    However, this in not a really a good example of where to use the switch statement either, since it would be far easier to do something like this:
    Code:
    <html xmlns = "http://www.w3.org/1999/xhtml">
    <head>
    <title>Excercise 8.11</title>
    <script type="text/javascript">
    onload = function(){
      var docWrite = function(text){
        var p = document.createElement('p');
        p.innerHTML = text;
        document.body.appendChild(p);
      };
      var things = [
        'A partridge in a pear tree',
        'Two turtle doves',
        'Three French hens',
        'Four calling birds',
        'Five golden rings'
      ];
      for (var day = 1; day <= 5; day++) {
        docWrite("On the "+day+ (["st", "nd"][day-1] || "th") 
         + " day of Christmas, my true love gave to me, " + things[day-1] + ".");
      };
    };
    </script>
    </head>
    <body>
    </body>
    </html>
    Last edited by ironboy; 10-06-2011 at 07:31 PM.

  • Users who have thanked ironboy for this post:

    Urbanmyth (10-06-2011)

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,306
    Thanks
    203
    Thanked 2,563 Times in 2,541 Posts
    document.write is indeed horrible but if the DOM methods of display suggested by ironboy are too advanced for you (or your teacher), then you can use another method innerHTML. I understand that the object of the exercise is to use a switch statement - as ironboy says, there are in fact better ways.

    Note that the script must be contained in a function called by body onload, as the <div> must exist before the script attempts to use it.

    Code:
    <html xmlns = "http://www.w3.org/1999/xhtml">
    <head>
    <title>Exercise 8.11</title>
    
    <script type = "text/javascript">
    function showSong() {
    var message = "";
    for (var day = 1; day <=5; day++) {
    switch (day) {
    
    case 1: message+=("On the "+day+"st day of Christmas, my true love gave to me, A partridge in a pear tree.<br>"); break;
    
    case 2: message+=("On the "+day+"nd day of Christmas, my true love gave to me, Two turtle doves,<br>"); break;
    
    case 3:message+=("On the "+day+"rd day of Christmas, my true love gave to me, Three French hens,<br>"); break;
    
    case 4: message+=("On the "+day+"th day of Christmas, my true love gave to me, Four calling birds,<br>"); break;
    
    case 5: message+=("On the "+day+"th day of Christmas, my true love gave to me, Five golden rings,<br>"); break;
    
    }
    }
    
    document.getElementById("song").innerHTML = message;
    }
    
    </script>
    </head>
    
    <body onload = "showSong()">
    
    <div id ="song"></div>
    
    </body>
    </html>
    "If you can't explain it simply, you don't understand it well enough”
    “Everything should be as simple as it is, but not simpler.”
    - both quotes Albert Einstein (German born American Physicist who developed the special and general theories of relativity. Nobel Prize for Physics in 1921. 1879-1955)
    Last edited by Philip M; 10-06-2011 at 08:20 PM. Reason: Typo

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    Urbanmyth (10-06-2011)

  • #4
    New to the CF scene
    Join Date
    Oct 2011
    Posts
    2
    Thanks
    2
    Thanked 0 Times in 0 Posts
    You guys are my heroes. Thank you so much, seriously appreciated!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •