...

View Full Version : Case Statement Goes to last case?



Redian
05-10-2007, 03:21 AM
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?


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";
}

kewlceo
05-10-2007, 03:57 AM
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

Redian
05-10-2007, 04:06 AM
Thank you! Now I feel really stupid, but at least this will not bother me. I can't believe I kept overlooking it.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum