Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Javascript calculation on form

1 view
Skip to first unread message

The Doctor

unread,
Jan 26, 2009, 11:40:07 AM1/26/09
to
Quick questions:

I have a script with a page that totals a price assuming quantity(X)=1 .

Customer have told me to go to quantity > 1 .

I did a test and I got a non-numeric answer and am scratching my head.

Page is found at https://secure.nl2k.ab.ca/pdsolutions/indexnongst.test.html .

Pointers please.
--
Member - Liberal International
This is doc...@nl2k.ab.ca Ici doc...@nl2k.ab.ca
God, Queen and country! Beware Anti-Christ rising!
Birthdate: 29 Jan 1969 Redhill Surrey England

Joe Fawcett

unread,
Jan 26, 2009, 12:10:35 PM1/26/09
to

"The Doctor" <doc...@doctor.nl2k.ab.ca> wrote in message
news:glkp17$c4j$2...@gallifrey.nk.ca...

I can't see where the error is but that code is going to be very hard to
maintain. Why not simplify it by looping through the table rows and adding
each quantity times price if the checkbox is checked?
Then if you add a new row there's no script change. All you need is to give
the table an ID to be able to get a reference to it (or use
getElementsByTagName if you know how many tables you have.

--

Joe

http://joe.fawcett.name

The Natural Philosopher

unread,
Jan 26, 2009, 1:21:52 PM1/26/09
to

Indeed. I have given up trying to predict where Javascript is going to
make a number a string and vice versa.

I just explicitly cast teh buggers.

RoLo

unread,
Jan 26, 2009, 3:29:16 PM1/26/09
to

your setting the input's .value which converts the numbers to
string...

Jonathan N. Little

unread,
Jan 28, 2009, 11:33:50 AM1/28/09
to
The Doctor wrote:
> Quick questions:
>
> I have a script with a page that totals a price assuming quantity(X)=1 .
>
> Customer have told me to go to quantity > 1 .
>
> I did a test and I got a non-numeric answer and am scratching my head.
>
> Page is found at https://secure.nl2k.ab.ca/pdsolutions/indexnongst.test.html .
>
> Pointers please.

When taking values from form fields explicitly convert numeric before
calculations

var qty = parseInt(document.someform.somefield.value);
var price = parseFloat(document.someform.somefield.value);


--
Take care,

Jonathan
-------------------
LITTLE WORKS STUDIO
http://www.LittleWorksStudio.com

RobG

unread,
Jan 29, 2009, 1:34:08 AM1/29/09
to
On Jan 27, 2:40 am, doc...@doctor.nl2k.ab.ca (The Doctor) wrote:
> Quick questions:
>
> I have a script with a page that totals a price assuming quantity(X)=1 .
>
> Customer have told me to go to quantity > 1 .

If the quantity can only ever be zero or one (and the page is coded so
that they are the effectively the only two values that can be used)
all you need is a checkbox, the quantity is irrelevant.

> I did a test and I got a non-numeric answer and am scratching my head.

I went for a walk and got a cup of tea and am reclining
peacfully. ;-)


> Page is found athttps://secure.nl2k.ab.ca/pdsolutions/indexnongst.test.html.
>
> Pointers please

Since your question is fundamentally about javascript, why not ask a
javascript group?

<URL: http://groups.google.com.au/group/comp.lang.javascript/topics?hl=en
>

Pointers:

1. Use a loop to collect values from the form, it is much, much
simpler.

2. Don't generate huge numbers of variables, use object properties or
an array. It makes dealing with them much simpler, e.g. you can loop
over them rather than explicity coding for each one.

3. Do not use expressions like:

var wprice1 = document.calc.price1.value = price1;
var wquantity1 = document.calc.quantity1.value = quantity1;

I don't think you know what that is doing.

5. Only decalre variables once. It doesn't hurt to declare them
multiple times, but it might get confusing.

4. To debug, create a very simple version of your page (say with just
two line items) and go from there. Get some debugging tools and ask
questions in an appropriate forum.

Anyhow, your problem is that you are declaring variables like
wquantity1, but only giving them a value if their associated checkbox
is checked (look at the else part of your if statements). However,
your total calculation uses them regardless of whether they have been
given a value or not.

If their checkbox isn't checked, wquantity1 et al have a value of
undefined, any number * undefined gives NaN (the special not-a-number
value). If any number in the calculation resolves to NaN, the whole
expression is NaN.

If you fix that (all 67 instances of it) your code will "work". See,
a loop would be easier. :-)

I suggest you seek help on how to write the script much more
efficiently.


--
Rob.

RobG

unread,
Jan 29, 2009, 1:37:18 AM1/29/09
to
On Jan 29, 2:33 am, "Jonathan N. Little" <lws4...@central.net> wrote:
> The Doctor wrote:
> > Quick questions:
>
> > I have a script with a page that totals a price assuming quantity(X)=1 .
>
> > Customer have told me to go to quantity > 1 .
>
> > I did a test and I got a non-numeric answer and am scratching my head.
>
> > Page is found athttps://secure.nl2k.ab.ca/pdsolutions/indexnongst.test.html.
>
> > Pointers please.
>
> When taking values from form fields explicitly convert numeric before
> calculations
>
> var qty = parseInt(document.someform.somefield.value);
> var price = parseFloat(document.someform.somefield.value);

To the OP:

Don't do either of those things, both are error prone.

You are much more likely to get useful answers if you ask in a group
specialising in the technology you are enquiring about.


--
Rob

RobG

unread,
Jan 29, 2009, 1:43:01 AM1/29/09
to
On Jan 29, 4:34 pm, RobG <rg...@iinet.net.au> wrote:
[...]

> Since your question is fundamentally about javascript, why not ask a
> javascript group?
>
> <URL:http://groups.google.com.au/group/comp.lang.javascript/topics?hl=en

Sorry about that, got confused where I was, thought I was replying to
another group...


--
Rob

Jonathan N. Little

unread,
Jan 29, 2009, 8:14:25 AM1/29/09
to
RobG wrote:

>> When taking values from form fields explicitly convert numeric before
>> calculations
>>
>> var qty = parseInt(document.someform.somefield.value);
>> var price = parseFloat(document.someform.somefield.value);
>
> To the OP:
>
> Don't do either of those things, both are error prone.
>

Why?

> You are much more likely to get useful answers if you ask in a group
> specialising in the technology you are enquiring about.

Agree, but OP was using the shotgun approach c.l.j. was in the list!

The Doctor

unread,
Jan 29, 2009, 8:50:08 AM1/29/09
to
In article <e556d$4981abb4$40cba7a4$25...@NAXS.COM>,

Shotgun / looking forthe best answer.

The Doctor

unread,
Jan 30, 2009, 9:51:04 PM1/30/09
to
In article <7810b4e0-92f8-4208...@v5g2000pre.googlegroups.com>,

RobG <rg...@iinet.net.au> wrote:
>On Jan 27, 2:40 am, doc...@doctor.nl2k.ab.ca (The Doctor) wrote:
>> Quick questions:
>>
>> I have a script with a page that totals a price assuming quantity(X)=1 .
>>
>> Customer have told me to go to quantity > 1 .
>
>If the quantity can only ever be zero or one (and the page is coded so
>that they are the effectively the only two values that can be used)
>all you need is a checkbox, the quantity is irrelevant.
>
>
>
>> I did a test and I got a non-numeric answer and am scratching my head.
>
>I went for a walk and got a cup of tea and am reclining
>peacfully. ;-)
>

A nice long walk always helps.

>
>> Page is found athttps://secure.nl2k.ab.ca/pdsolutions/indexnongst.test.html.
>>
>> Pointers please
>
>Since your question is fundamentally about javascript, why not ask a
>javascript group?
>
><URL: http://groups.google.com.au/group/comp.lang.javascript/topics?hl=en
>>
>
>Pointers:
>
>1. Use a loop to collect values from the form, it is much, much
>simpler.

You are not going to like me. I have seen this in many programming
languages but not Java/Javascript. How do you start a loop?

>
>2. Don't generate huge numbers of variables, use object properties or
>an array. It makes dealing with them much simpler, e.g. you can loop
>over them rather than explicity coding for each one.
>

So can one do that in said form?

>3. Do not use expressions like:
>
> var wprice1 = document.calc.price1.value = price1;
> var wquantity1 = document.calc.quantity1.value = quantity1;
>
>I don't think you know what that is doing.

This is what you get when reading the web

>
>5. Only decalre variables once. It doesn't hurt to declare them
>multiple times, but it might get confusing.
>

Once works for me.

>4. To debug, create a very simple version of your page (say with just
>two line items) and go from there. Get some debugging tools and ask
>questions in an appropriate forum.
>

How so in Javascript?

>Anyhow, your problem is that you are declaring variables like
>wquantity1, but only giving them a value if their associated checkbox
>is checked (look at the else part of your if statements). However,
>your total calculation uses them regardless of whether they have been
>given a value or not.
>

Ugh.

>If their checkbox isn't checked, wquantity1 et al have a value of
>undefined, any number * undefined gives NaN (the special not-a-number
>value). If any number in the calculation resolves to NaN, the whole
>expression is NaN.
>

Thank you about NaN.

>If you fix that (all 67 instances of it) your code will "work". See,
>a loop would be easier. :-)
>
>I suggest you seek help on how to write the script much more
>efficiently.
>

Loops and arrays makes sense.

Any web pointers to this?

>
>--
>Rob.

Mark Hansen

unread,
Jan 31, 2009, 11:05:19 AM1/31/09
to
On 01/30/09 18:51, The Doctor wrote:
> In article <7810b4e0-92f8-4208...@v5g2000pre.googlegroups.com>,
> RobG <rg...@iinet.net.au> wrote:
>>On Jan 27, 2:40�am, doc...@doctor.nl2k.ab.ca (The Doctor) wrote:
>>> Quick questions:
>>>
>>> I have a script with a page that totals a price assuming quantity(X)=1 .
>>>
>>> Customer have told me to go to quantity > 1 .
>>
>>If the quantity can only ever be zero or one (and the page is coded so
>>that they are the effectively the only two values that can be used)
>>all you need is a checkbox, the quantity is irrelevant.
>>
>>
>>
>>> I did a test and I got a non-numeric answer and am scratching my head.
>>
>>I went for a walk and got a cup of tea and am reclining
>>peacfully. ;-)
>>
>
> A nice long walk always helps.
>
>>
>>> Page is found athttps://secure.nl2k.ab.ca/pdsolutions/indexnongst.test.html.
>>>
>>> Pointers please
>>
>>Since your question is fundamentally about javascript, why not ask a
>>javascript group?
>>
>><URL: http://groups.google.com.au/group/comp.lang.javascript/topics?hl=en
>>>
>>
>>Pointers:
>>
>>1. Use a loop to collect values from the form, it is much, much
>>simpler.
>
> You are not going to like me. I have seen this in many programming
> languages but not Java/Javascript. How do you start a loop?

Are you trying to write JavaScript without a JavaScript reference (book)?
I have the O'Reilly book: JavaScript: The Definitive Guide, which shows
(among many, *many* other things) several looping constructs, like

for (initialize; test; increment)
statement

or

initialize;
while (test) {
statement;
increment;
}

0 new messages