Reply
JavaScript Arithmetic Problem
Old 03-25-2008, 01:28 PM JavaScript Arithmetic Problem
Average Talker

Posts: 19
Location: Huddersfield, UK
Hi,

My JavaScript knowledge is very poor (I'm a layout artist(!!)) so could someone have a look at this for me and let me know what's going on.

http://www.lsi-gifts.co.uk/dev/jscalc/

The first two answer boxes work perfectly. However, I've been playing about with the third answer box and cannot get it to give the correct answer (which is 12.6 with the default values).

Can someone take a look at it for me and let me know what's going on. The formula I want to use is as follows:

(30f * (b+d) * (a + c)) / 1000

I'm not doing any of the others as the rest of the answers revolve around the same formula so sod that until I get this one right!

I look forward to your replies.

Cheers,

Scott.
tuestunim is offline
Reply With Quote
View Public Profile
 
When You Register, These Ads Go Away!
     
Old 03-26-2008, 04:16 PM Re: JavaScript Arithmetic Problem
willcode4beer's Avatar
Webmaster Talker

Posts: 695
Name: Paul Davis
Location: San Francisco
It would probably be work out if you used the same equation in your javascript that you do in this post.
Code:
eval(
(calc.b.value + calc.d.value) * (calc.a.value + calc.c.value) * (30 * calc.f.value) / 1000
);
You're missing some parenthesis
Code:
eval(
( (calc.b.value + calc.d.value) * (calc.a.value + calc.c.value) * (30 * calc.f.value) )/ 1000
);
__________________
Paul Davis
willCode4Beer.com (coding for all the right reasons)

Last edited by willcode4beer : 03-26-2008 at 04:19 PM.
willcode4beer is offline
Reply With Quote
View Public Profile
 
Old 03-27-2008, 12:40 PM Re: JavaScript Arithmetic Problem
Average Talker

Posts: 19
Location: Huddersfield, UK
Thanks for your help but it's still throwing out the same, wrong, answer I'm afraid. It's giving out 5949.9 rather than 12.6. I admit that it makes the product look very good but it's somewhat misleading!!

Any other ideas?

Cheers,

Scott.
tuestunim is offline
Reply With Quote
View Public Profile
 
Old 03-27-2008, 09:21 PM Re: JavaScript Arithmetic Problem
willcode4beer's Avatar
Webmaster Talker

Posts: 695
Name: Paul Davis
Location: San Francisco
doh!, I got hit with the oldest one in the book.
The string arithmetic was throwing it off (gotta convert those strings to numbers).
Try this:
Code:
eval(
  (
        (Number(calc.b.value) + Number(calc.d.value))
     * (Number(calc.a.value) + Number(calc.c.value))
     * (30 * Number(calc.f.value))
  )/ 1000
);
The new lines were just added for clarity
__________________
Paul Davis
willCode4Beer.com (coding for all the right reasons)
willcode4beer is offline
Reply With Quote
View Public Profile
 
Old 03-28-2008, 04:54 AM Re: JavaScript Arithmetic Problem
Average Talker

Posts: 19
Location: Huddersfield, UK
Thank you very much! All sorted now.
tuestunim is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to JavaScript Arithmetic Problem
 

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 


Page generated in 0.12950 seconds with 13 queries