By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,842 Members | 2,440 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,842 IT Pros & Developers. It's quick & easy.

HELP: Why won't this javascript work?

P: n/a
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>

Sep 29 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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;

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

Sep 29 '05 #2

P: n/a
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

Sep 29 '05 #3

P: n/a
John wrote:
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>

[...]
--
Rob
Sep 30 '05 #4

P: n/a
John <im@home.net> wrote in message news:7s********************************@4ax.com...
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.
Sep 30 '05 #5

P: n/a
Lee
Stephen Chalmers said:

John <im@home.net> wrote in message
news:7s********************************@4ax.com.. .
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];

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;

Sep 30 '05 #6

P: n/a
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.

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

Sep 30 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.