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 3 of 3
  1. #1
    New to the CF scene
    Join Date
    May 2007
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Case Statement Goes to last case?

    I'm trying to make a google gadgets binary clock, and my time is limited so I didn't search the forums for errors similar to mine (cliche response to that proposition, sorry). I'm new to javascript, and have never used it before. I used an alert to find out that no matter what it runs the code for case 12. Is there something simple that I'm doing wrong?

    Code:
      var currentTime = new Date()
        Hours16.src = "LightOff.png"
        Hours8.src = "LightOff.png"
        Hours4.src = "LightOff.png"
        Hours2.src = "LightOff.png"
        Hours1.src = "LightOff.png"
        Minutes4.src = "LightOff.png"
        Minutes1.src = "LightOff.png"
        Minutes2.src = "LightOff.png"
        Minutes8.src = "LightOff.png"
        Minutes16.src = "LightOff.png"
        Minutes32.src = "LightOff.png"
        Seconds32.src = "LightOff.png"
        Seconds16.src = "LightOff.png"
        Seconds8.src = "LightOff.png"
        Seconds4.src = "LightOff.png"
        Seconds2.src = "LightOff.png"
        Seconds1.src = "LightOff.png"
        Month8.src = "LightOff.png"
        Month4.src = "LightOff.png"
        Month2.src = "LightOff.png"
        Month1.src = "LightOff.png"
        Day16.src = "LightOff.png"
        Day8.src = "LightOff.png"
        Day4.src = "LightOff.png"
        Day2.src = "LightOff.png"
        Day1.src = "LightOff.png"
    
    var month = 1 + currentTime.getMonth()
    switch(month){
    case  1:
      Month1.src = "LightOn.png";
    case 2:
      Month2.src = "LightOn.png";
    case 3:
      Month2.src = "LightOn.png"
      Month1.src = "LightOn.png";
    case 4:
      Month4.src = "LightOn.png";
    case 5:
      Month4.src = "LightOn.png"
      Month1.src = "LightOn.png";
    case 6:
      Month4.src = "LightOn.png"
      Month2.src = "LightOn.png";
    case 7:
      Month4.src = "LightOn.png"
      Month2.src = "LightOn.png"
      Month1.src = "LightOn.png";
    case 8:
      Month8.src = "LightOn.png";
    case 9:
      Month8.src = "LightOn.png"
      Month1.src = "LightOn.png";
    case 10:
      Month8.src = "LightOn.png"
      Month2.src = "LightOn.png";
    case 11:
      Month8.src = "LightOn.png"
      Month2.src = "LightOn.png"
      Month1.src = "LightOn.png";
    case 12:
      Month8.src = "LightOn.png"
      Month4.src = "LightOn.png";
    }

  • #2
    Regular Coder kewlceo's Avatar
    Join Date
    Mar 2006
    Location
    California, US
    Posts
    484
    Thanks
    1
    Thanked 3 Times in 3 Posts
    You need a "break" within each case to keep the code from automatically slipping into the next case.

    http://www.javascriptkit.com/javatutors/switch.shtml
    UBERHOST.NET
    Shared, reseller, semidedicated hosting and dedicated server plans.
    DirectAdmin • Installatron • Money-Back Guarantee • 24/7 Support
    Providing "Service Above All Else" since 2005.

  • #3
    New to the CF scene
    Join Date
    May 2007
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you! Now I feel really stupid, but at least this will not bother me. I can't believe I kept overlooking it.


  •  

    Posting Permissions

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