Whenever the following script is called, it always defaults to the default CASE (14.99)
Why won't it recognise the other range of values?
Please I need help.
<script language="javascript" type="text/javascript">
function calculatecost() {
var rockets=eval(document.forms[0].Rocket_Qty.value);
var fairies=eval(document.forms[0].Fairy_Qty.value);
var postage;
var costperitem;
var totalitems=rockets+fairies;
switch (totalitems) {
case (totalitems>19):
costperitem=6;
break;
case ((totalitems>14) && (totalitems<20)):
costperitem=6.50;
break;
case ((totalitems>9) && (totalitems<15)):
costperitem=7;
break;
case ((totalitems>4) && (totalitems<10)):
costperitem=7.50;
break;
default: costperitem=14.99;
}
if (document.forms[0].ordertype.options[1].selected) postage=2;
else postage=4.95;
if (totalitems<=0) document.forms[0].totalcost.value=0;
else
document.forms[0].totalcost.value=(totalitems*costperitem+postage).toFixed(2);
}
</script>
Evertjan. - 29 Sep 2005 21:30 GMT
John wrote on 29 sep 2005 in comp.lang.javascript:
> switch (totalitems) {
> case (totalitems>19):
[quoted text clipped - 10 lines]
> break;
> default: costperitem=14.99;
Try:
switch (true) {
case (totalitems>19):
costperitem=6;
break;
case ((totalitems>14) && (totalitems<20)):
costperitem=6.50;
break;
case ((totalitems>9) && (totalitems<15)):
costperitem=7;
break;
case ((totalitems>4) && (totalitems<10)):
costperitem=7.50;
break;
default: costperitem=14.99;
}
btw, switch-case is a difficult construct
that can always by exchanged by if-then-else:
[and use <= to have less chance for mistakes]
if (totalitems>19)
costperitem=6;
else if ((totalitems>14) && (totalitems<=19))
costperitem=6.50;
else if ((totalitems>9) && (totalitems<=14))
costperitem=7;
else if ((totalitems>4) && (totalitems<=9))
costperitem=7.50;
else // totalitems<=4
costperitem=14.99;

Signature
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)
Lee - 29 Sep 2005 21:34 GMT
John said:
>Whenever the following script is called, it always defaults to the default CASE
>(14.99)
>Why won't it recognise the other range of values?
>
>Please I need help.
><script language="javascript" type="text/javascript">
> function calculatecost() {
> var rockets=eval(document.forms[0].Rocket_Qty.value);
> var fairies=eval(document.forms[0].Fairy_Qty.value);
Don't use eval() to get the value of a form field.
var rockets=+document.forms[0].Rocket_Qty.value;
> var postage;
> var costperitem;
> var totalitems=rockets+fairies;
> switch (totalitems) {
> case (totalitems>19):
> costperitem=6;
Don't use boolean expressions as case labels.
A Switch construct is a poor choice in this case, anyway.
Use a simple series of "if" statements
RobG - 30 Sep 2005 04:01 GMT
> Whenever the following script is called, it always defaults to the default CASE (14.99)
> Why won't it recognise the other range of values?
[quoted text clipped - 5 lines]
> var rockets=eval(document.forms[0].Rocket_Qty.value);
> var fairies=eval(document.forms[0].Fairy_Qty.value);
As noted above, don't use eval. Text input values are always returned
as strings, so 'rockets' and 'fairies' are strings. To make them
numbers, use a unary '+':
var rockets = +document.forms[0].Rocket_Qty.value;
var fairies = +document.forms[0].Fairy_Qty.value;
rockets and fairies are now numbers.
> var postage;
> var costperitem;
> var totalitems=rockets+fairies;
The '+' operator can mean many things (it's overloaded). When used
between two strings, it means 'concatenate'. If you haven't used the
unary operator above when getting the values, you can use it now:
var totalitems = +rockets + +fairies;
In this case, if rockets and fairies were strings, they'll remain
strings but the value of totalitems is assigned the sum of their values
as if they were numbers.
This kind of type conversion happens under other circumstances too, e.g.
even if you haven't converted rockets to a number, it will still be
treated as a number (if it can be) in the following:
if ( rockets < 20 ){
...
as will:
if ( document.forms[0].Rocket_Qty.value < 20 ){
...
More good stuff:
<URL:http://www.jibbering.com/faq/#FAQ4_21>
[...]

Signature
Rob
Stephen Chalmers - 30 Sep 2005 11:52 GMT
> Whenever the following script is called, it always defaults to the default CASE (14.99)
> Why won't it recognise the other range of values?
>
> Please I need help.
If you don't like multiple if-else statements, you can
maintain a table of ranges and associated values:
var costPerItem=0, total_Items=/* value read */ ;
var ranges=
[
total_Items>19, 6,
total_Items>14 && total_Items<20, 6.5,
total_Items>9 && total_Items<15 , 7 ,
total_Items>4 && total_Items<10, 7.5,
total_Items<=4, 14.99
];
for(var i=0,ln=ranges.length*2; i<ln && !ranges[i]; i+=2)
;
costPerItem=ranges[i+1];
--
S.C.
Lee - 30 Sep 2005 14:48 GMT
Stephen Chalmers said:
>>Whenever the following script is called, it always defaults to the default CASE
>>(14.99)
[quoted text clipped - 20 lines]
>
>costPerItem=ranges[i+1];
My personal preference is:
costperitem = 14.99;
if (totalitems > 4) costperitem = 7.5;
if (totalitems > 9) costperitem = 7;
if (totalitems > 14) costperitem = 6.5;
if (totalitems > 19) costperitem = 6;
Evertjan. - 30 Sep 2005 15:32 GMT
Lee wrote on 30 sep 2005 in comp.lang.javascript:
> My personal preference is:
>
[quoted text clipped - 3 lines]
> if (totalitems > 14) costperitem = 6.5;
> if (totalitems > 19) costperitem = 6;
My personal preference is:
costperitem =
(totalitems > 19) ? 6 :
(totalitems > 14) ? 6.5 :
(totalitems > 9) ? 7 :
(totalitems > 4) ? 7.5 :
14.99;
It is quicker, more concise end and more js-ish.

Signature
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)