HELP: Why won't this javascript work?

J

John

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>
 
E

Evertjan.

John wrote on 29 sep 2005 in comp.lang.javascript:
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;

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;
 
L

Lee

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
 
R

RobG

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);

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>

[...]
 
S

Stephen Chalmers

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.
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+=2)
;

costPerItem=ranges[i+1];
 
L

Lee

Stephen Chalmers said:
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.
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+=2)
;

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;
 
E

Evertjan.

Lee wrote on 30 sep 2005 in comp.lang.javascript:
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;

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top