473,326 Members | 2,192 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

JavaScript can't do math!?!?

Does anybody know why these functions seem to be multiplying things by
10 or 100? It must be something obvious, but I can't track it down.

function multiConvert(n, spra, sm, spoa, dpra, dm, dpoa, inv) {
var z;
if (inv == 1) {
z = ((n+spra)/sm)+spoa;
return ((z-dpoa)*dm)-dpra;
} else {
z = ((n+spra)*sm)+spoa;
return ((z-dpoa)/dm)-dpra;
}
}
function doConvert(idx, max) {
var i, pa1, m1, po1, n1, pa2, m2, po2, n2, inv;
inv = document.forms[1].elements[0].value;
n1 = document.forms[1].elements[idx*4 + 4].value;
pa1 = document.forms[1].elements[idx*4 + 1].value;
m1 = document.forms[1].elements[idx*4 + 2].value;
po1 = document.forms[1].elements[idx*4 + 3].value;
if (n1 != "") {
for (i = 0; i <= max; i++) {
if (i != idx) {
pa2 = document.forms[1].elements[i*4 + 1].value;
m2 = document.forms[1].elements[i*4 + 2].value;
po2 = document.forms[1].elements[i*4 + 3].value;
n2 = multiConvert(n1,pa1,m1,po1,pa2,m2,po2,inv);
document.forms[1].elements[i*4 + 4].value = n2;
}
}
}
}

--
Hasta la pasta,
Jonathyn Bet'nct
-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----
Jul 23 '05 #1
2 1524
On Thu, 26 Aug 2004 23:51:00 -0700, Jonathyn Bet'nct
<jo******@napanet.net> wrote:
Does anybody know why these functions seem to be multiplying things by
10 or 100? It must be something obvious, but I can't track it down.
Probably[1] because you're concatenating strings, rather than adding
numbers. The value property of form elements holds strings. Whilst
subtraction, division, and multiplication operations between two strings
results in the automatic conversion to number, no such conversion occurs
with addition (one operand would have to be a number).

The solution is to force conversion for all form element values that
represent numbers. If the form elements are text boxes, you should ensure
that the values are actually numbers before converting. This is best
accomplished with regular expressions.

Simple conversion to number is through the unary plus operator. For
example,

inv = document.forms[1].elements[0].value;
if( <inv is not number> ) {
// exit function
}
inv = +inv; // convert to number

[snipped multiConvert]
function doConvert(idx, max) {
var i, pa1, m1, po1, n1, pa2, m2, po2, n2, inv;
inv = document.forms[1].elements[0].value;
n1 = document.forms[1].elements[idx*4 + 4].value;
pa1 = document.forms[1].elements[idx*4 + 1].value;
m1 = document.forms[1].elements[idx*4 + 2].value;
po1 = document.forms[1].elements[idx*4 + 3].value;


You would be better off storing a reference to the form, rather than
performing a look up again, and again.

var form = document.forms[1],
inv = form.elements[0].value,
n1 = form.elements[idx*4 + 4].value;
// etc

[snipped rest of function]

Hope that helps,
Mike
[1] A URI to a working example would make certain.

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #2
Michael Winter <M.******@blueyonder.co.invalid> wrote:
On Thu, 26 Aug 2004 23:51:00 -0700, Jonathyn Bet'nct
<jo******@napanet.net> wrote:
Does anybody know why these functions seem to be multiplying things by
10 or 100? It must be something obvious, but I can't track it down.
Probably[1] because you're concatenating strings, rather than adding
numbers. The value property of form elements holds strings. Whilst
subtraction, division, and multiplication operations between two strings
results in the automatic conversion to number, no such conversion occurs
with addition (one operand would have to be a number).

The solution is to force conversion for all form element values that
represent numbers. If the form elements are text boxes, you should ensure
that the values are actually numbers before converting. This is best
accomplished with regular expressions.

Simple conversion to number is through the unary plus operator.


Thank you. I found out late last night that it was because of
concatenation, but I solved it by multiplying by 1. The unary plus
operator is probably better, so I changed it to use that.
You would be better off storing a reference to the form, rather than
performing a look up again, and again.
I've also done this. It appears to be a little faster.
Hope that helps,
Mike
It does, definitely. Thank you so much.
[1] A URI to a working example would make certain.


http://mika.000k.net/cgi-bin/multiconvert.pl

The server has currently exceeded bandwidth, so here's another URL that
might work:

http://kreativekorp.dyndns.org/cgi-bin/multiconvert.pl

--
Hasta la pasta,
Jonathyn Bet'nct
-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----
Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: VictorG | last post by:
Hello, I am new to JS and am trying to add some HTML into a JS function. So that when called the script as well as the HTML will be invoked. Is there some type of embed mechanism, sort of the...
4
by: frogman042 | last post by:
My daughter is playing around trying to learn JavaScript and she wrote a small program that prints out a message in increasing and decreasing font size and color changes. She is using document...
2
by: Head In A Pan | last post by:
Hello! My JavaScripting is at novice level - and after completing a tutorial on a 'floating layer' I was proud that I got it working on Firefox, Safari, Camino & even IE5 for mac!!! But not IE7!...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.