calculate function: whats up with this then?
Posted: Mon Jun 06, 2005 8:22 am
I've been trying to write a reasonably simply cost calulator, based on two levels of service, and a price drop the more of an item you purchase. When you hit submit the calculate() function should execute. I hate working with javascript because very often nothing happens! I just don't seem to be able to reassign values to variables and work with them. I get no error, nothing! Out of all the languages I've used javascript always has me stumped! Can anyone tell me why this doesn't work?
Code: Select all
function calculate(){
var servicelvl = document.calc.service.value;
var boxnum = document.calc.boxno.value;
var perbox;
if (servicelvl == 1){
switch(boxnum){
case (boxnum > 0 && boxnum <=55):
perbox = 55;
break;
case (boxnum > 55 && boxnum <=250):
perbox = 0.60;
break;
case (boxnum > 250 && boxnum <= 500):
perbox = 0.55;
break;
case (boxnum >500 && boxnum <= 750):
perbox = 0.50;
break;
case (boxnum >750 && boxnum <= 1000):
perbox = 0.45;
break;
case (boxnum > 1000):
perbox = 0.40;
break;
default:
perbox = 0;
}
}
else if (servicelvl == 2){
switch(boxnum){
case (boxnum > 0 && boxnum <=55):
perbox = 20;
break;
case (boxnum > 55 && boxnum <=250):
perbox = 0.40;
break;
case (boxnum > 250 && boxnum <= 500):
perbox = 0.35;
break;
case (boxnum >500 && boxnum <= 750):
perbox = 0.30;
break;
case (boxnum >750 && boxnum <= 1000):
perbox = 0.25;
break;
case (boxnum > 1000):
perbox = 0.20;
break;
default:
perbox = 0;
}
}
document.calc.answer.value = eval(boxnum * perbox);
}