Results 1 to 2 of 2

Thread: Need help with switch structure/repetition (JavaScript)

  1. #1
    jcquint Guest

    Need help with switch structure/repetition (JavaScript)

    Hi guys, college student here having some trouble with the JavaScript portion of my computer science class. I'm almost done with this week's assignment, but this one problem is giving me fits.

    It asks me to wite a script that uses repetition and a switch structure to print the song 'The Twelve Days of Christmas.'

    I somewhat understand what it is trying to get me to do... something along the lines of, case "1": A partridge in a pear tree. case "2": case "1" + two turtle doves...

    And I also know that I should probably make a variable for each "day".. I just can't wrap my head around the problem as a whole. Please help me. I'm not some lazy student on here begging yall to do my homework, I just can't get help from my professor since the class is being taken in and online format and can't afford a tutor! I've worked hard on this and hate to hit this wall.

    Thanks in advance guys...

  2. #2
    Join Date
    Mar 2010
    Location
    England
    Posts
    1,144
    Well its difficult to show you how you could do it, without actually doing it for you. I would do it something like this, although i would use an array of phrases from each of the 12 days of the song. And i would calculate the length of it rather than hand coding etc..

    Basically, if you created a for() loop, ( as you want it repetitive ),obviously you have 12 days of Christmas, so you want to repeat it 12 times. Each day, 1 - 2 - 3 - 4 - 5 - 6 ... etc.. You would have a switch statement do detect the day and add the appropriate phrase, like this:
    Code:
    var curday = 12;
    var song = '';
    
    for( curday = 2; curday > 0; curday-- ){
      switch( curday ){
        case 1:
             song += 'A partridge in a pear tree' + '<br/>';
        break;
        case 2:
             song += 'Two Turtle Doves' + '<br/>';
        break;
      }
    }
    document.write( song );

Similar Threads

  1. JavaScript Collector
    By JavaScriptBank in forum Client & Server Side Scripting (PHP, ASP, JavaScript)
    Replies: 0
    Last Post: 09 Jul 2009, 03:28 AM
  2. JavaScript library
    By JavaScriptBank in forum Client & Server Side Scripting (PHP, ASP, JavaScript)
    Replies: 0
    Last Post: 09 Jul 2009, 01:26 AM
  3. javascript to switch style sheet?
    By electrickeye in forum Web Design, HTML Reference and CSS
    Replies: 1
    Last Post: 16 Apr 2008, 11:50 AM
  4. element specific javascript
    By gumbo in forum Client & Server Side Scripting (PHP, ASP, JavaScript)
    Replies: 0
    Last Post: 04 Dec 2005, 06:25 PM
  5. element specific javascript
    By gumbo in forum Web Design, HTML Reference and CSS
    Replies: 0
    Last Post: 04 Dec 2005, 06:23 PM

Posting Permissions

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