473,405 Members | 2,176 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,405 software developers and data experts.

Why doesn't this work?? (Adding variables)

Here's my code:
function doMath() {

var grandTot=0;
var one=new Array(13);
var two=new Array(13);
var prod=new Array(13);
one[1] = eval(document.form1.BFPpric.value);
two[1] = eval(document.form1.BFPquantity.value);
one[2] = eval(document.form1.BFTTpric.value);
two[2] = eval(document.form1.BFTTquantity.value);

etc down to 13 then

for (var i = 1; i < 14; i++) {
prod[i] = one[i] * two[i];
grandTot += prod[i];
}
}

There will always be some prod[i] in there that will be 0 (zero)
value, because not every item is ordered.
grandTot always totals to 0 (zero)!!!!! WHY??
It's making me nuts!!

Nov 14 '06 #1
4 1301
VK

ri**@away32.com wrote:
Here's my code:
function doMath() {

var grandTot=0;
var one=new Array(13);
var two=new Array(13);
var prod=new Array(13);
one[1] = eval(document.form1.BFPpric.value);
two[1] = eval(document.form1.BFPquantity.value);
one[2] = eval(document.form1.BFTTpric.value);
two[2] = eval(document.form1.BFTTquantity.value);

etc down to 13 then

for (var i = 1; i < 14; i++) {
prod[i] = one[i] * two[i];
grandTot += prod[i];
}
}

There will always be some prod[i] in there that will be 0 (zero)
value, because not every item is ordered.
grandTot always totals to 0 (zero)!!!!! WHY??
Mostly because eval(string) has nothing to do with math. You maybe were
thinking of parseInt or parseFloat.

Nov 14 '06 #2

ri**@away32.com wrote:
Here's my code:
function doMath() {

var grandTot=0;
var one=new Array(13);
var two=new Array(13);
var prod=new Array(13);
one[1] = eval(document.form1.BFPpric.value);
two[1] = eval(document.form1.BFPquantity.value);
one[2] = eval(document.form1.BFTTpric.value);
two[2] = eval(document.form1.BFTTquantity.value);

etc down to 13 then

for (var i = 1; i < 14; i++) {
prod[i] = one[i] * two[i];
grandTot += prod[i];
}
}

There will always be some prod[i] in there that will be 0 (zero)
value, because not every item is ordered.
grandTot always totals to 0 (zero)!!!!! WHY??
It's making me nuts!!
I think VK explained pretty nicely, but I wanted to add a tip that may
save you going nuts in future.

Ideally, test just one line of code or portions of it. So for example
in your code, the thing to check before going further is:

eval(document.form1.BFPpric.value);

And if you kept getting 0 as an answer regardless of what value you put
in your form field, you would know that there maybe something wrong
with the way you're trying to retrieve the values.

This would probably have led you to the conclusion that eval() is not
giving you the answers you expect and you'd have searched the net for
its proper use or for an alternative way of extracting form field
values.

It may seem trivial to you at this point, but believe me, when your
scripting starts to get more and more complex, you'll appreciate this
kind of approach.

Nov 14 '06 #3
ri**@away32.com wrote:
Here's my code:
function doMath() {

>
var grandTot=0;
var one=new Array(13);
var two=new Array(13);
var prod=new Array(13);
var one=[],two=[],prod=[]
one[1] = eval(document.form1.BFPpric.value);
two[1] = eval(document.form1.BFPquantity.value);
one[2] = eval(document.form1.BFTTpric.value);
two[2] = eval(document.form1.BFTTquantity.value);
one[1] = +(document.form1.BFPpric.value);
// Attempt to cast value as a number
>
etc down to 13 then
var V=one.length;
for (var i = 1; i <= V; i++) //Number of products may change over time
for (var i = 1; i < 14; i++) {


if(!one[i] || !two[i] || isNaN(one[i]) || isNaN(two[i])) {continue}

//you need to do some checking of inputs, and remember multiplication by
zero results in zero

Mick
prod[i] = one[i] * two[i];
grandTot += prod[i];
}
}

There will always be some prod[i] in there that will be 0 (zero)
value, because not every item is ordered.
grandTot always totals to 0 (zero)!!!!! WHY??
It's making me nuts!!
Nov 14 '06 #4
bheku wrote:
ri**@away32.com wrote:
<snip>
>one[1] = eval(document.form1.BFPpric.value);
two[1] = eval(document.form1.BFPquantity.value);
one[2] = eval(document.form1.BFTTpric.value);
two[2] = eval(document.form1.BFTTquantity.value);

etc down to 13 then

for (var i = 1; i < 14; i++) {
prod[i] = one[i] * two[i];
grandTot += prod[i];
}
}
<snip>
>grandTot always totals to 0 (zero)!!!!! WHY??
It's making me nuts!!

I think VK explained pretty nicely,
That is unfortunate for you, as VK missed the mark by a long way and
instead fixated on an unrelated issue. While it is inadvisable to use
the - eval - method to convert numeric strings to numbers it is not
inadvisable because it doesn't work (rather it is inadvisable because
it is the least efficient method of doing so, and has completely
unpredictable results when the input is not string representations of
numbers), and its use will not explain why the total value of -
grandTot - is zero (and not NaN, which would be expected from most
circumstances when the input was not in the form of numeric strings).

Richard.

Nov 14 '06 #5

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

Similar topics

19
by: Chris Allen | last post by:
Hi I'm new to PHP and I'm trying to create a Login Form. Once the user has logged in then he shouldn't have to log in again. The trouble is I'm getting a new session ID between every page and so...
1
by: Erick Bodine | last post by:
I am trying to set a new environment variable on a W2k machine with only partial success. The name("SSID") and value("ASIM") show up correctly in the registry and when I go to "System...
15
by: AV | last post by:
Hallo any idea why the following code doesn't work? ////////////////////////////////// function myfunc(){ with(this){ prop="hallo world"; } }
12
by: Rhino | last post by:
I am having an odd problem: the sqlj command on my system doesn't work. I am running DB2 (LUW) V8 (FP8) on WinXP. I haven't done an sqlj program since Version 6 of DB2 (LUW) so I checked the...
4
by: Damien | last post by:
Hi guys, Before I spend hours barking up the wrong tree, am I right in thinking that there is no way for a non ASP.NET page to POST to a ASP.NET page and be able to retrieve the variables in any...
3
by: iwl | last post by:
Hello, I would like to add Variables to my embedded python which represents variables from my C++-Programm. I found C-Api-funcs for adding my C-Funcs to python but none to add variables. I...
6
by: Johnny Jörgensen | last post by:
I've got a usercontrol derived from a normal ComboBox that contains some special formatting code. On my main form I've got a lot of my custom comboboxes. I discovered a bug in the derived...
33
by: sophia.agnes | last post by:
Dear all, consider the following program #include<stdio.h> #include<unistd.h> main() { int pid,*i,j;
39
by: alex | last post by:
I've converted a latin1 database I have to utf8. The process has been: # mysqldump -u root -p --default-character-set=latin1 -c --insert-ignore --skip-set-charset mydb mydb.sql # iconv -f...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.