Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Function won't add 1

Status
Not open for further replies.

swpsoak

Technical User
Mar 5, 2002
15
US
Below is the HTML code for doing some calculations (this has been whittled down to the bear necessities).
The form requires two inputs, then “Calculates” the remaining four values.
Everything runs fine using the expression f.M.value = eval (f.OD.value * 3); but this is not the correct expression that is needed, it’s just a test expression.
If the correct expression, f.M.value = eval (f.OD.value + (3 * W) - (1.5155 / NT) ); is used instead, it returns nothing and shows an “Error on page.” down on the status bar.

The problem seems to lie in the addition (+) portion of the expression.

For testing purposes use:
OD = .625
NT = 11

CALCULATE - This will return:

P = 0.091
PD = 0.56595
W = 0.05254
and M = 1.875

If the f.M.value expression is changed to the correct one, it should return 0.6448.

(alerts are just in there for testing).

Code:
<HTML>
<HEAD>
<TITLE>3 WIRE CALCULATIONS</TITLE>
</HEAD>

<SCRIPT language=javascript>
<!--- Hide script from old browsers
function compute(f)
	 {
	f.P.value=(eval (1 / f.NT.value).toFixed(3) );
	//alert ("P = " + f.P.value );  
	
	f.PD.value = (eval (f.OD.value -(.6495 / f.NT.value)).toFixed(5) );
	//alert ("PD = " + f.PD.value ); 

	f.W.value = (eval (.57735 * f.P.value).toFixed(5) );
	//alert ("W = " + f.W.value );  
	
	//This is the correct expression
	//f.M.value = eval (f.OD.value + (3 * W) - (1.5155 / NT) );

	//This expression is for testing
	f.M.value = (eval (f.OD.value * 3));
	alert ("M = " + f.M.value );

	}
// end hiding from old browsers -->
</SCRIPT>

<BODY>
<CENTER>

<FORM>
           
<TABLE cellSpacing=0 cellPadding=1 border=2 width=45% bgcolor=#cccccc>

<TR>
<TD colspan=4 bgColor=#330099 align=center><B><FONT face="Arial" color=#ffffff size=2>3 WIRE</FONT></B></TD>
</TR>

<TR>
<TD ALIGN=CENTER colspan=4><B><FONT face="Arial" size=2>Thread Input Dimensions:</FONT></B></TD>
</TR>

<TR>
<TD align=center><FONT face="Arial" size=2>Threads Size:</FONT></TD>
<TD align=center><FONT size=1>5/8 = .625</font><BR>
<FONT face="Arial" size=1 color=red>(OD)</FONT></TD>
<TD align=center><FONT size=2>
	<INPUT name=OD size=10> </FONT></TD>
</TR>

<TR>
<TD align=center><FONT face="Arial" size=2>No. of Threads per inch (TPI):</FONT></TD>
<TD align=center><FONT size=1 color=red>(NT)</FONT></TD>
<TD align=center><FONT size=2>
	<INPUT name=NT size=10> </FONT></TD>
</TR>

<TR>
<TD colspan=3 align=center>
	<INPUT onclick=compute(this.form) type=button value=Calculate name=Calculate> 
	<INPUT type=reset value="  Clear  " name=name>
</TD>
</TR>

<TR>
<TD colSpan=3 align=center><B><FONT  face="Arial" size=2>Resulting Thread Information:</FONT></B></TD>
</TR>

<TR>
<TD align=center><FONT face="Arial" size=2>Pitch:</FONT></TD>
<TD align=center><FONT size=1 color=#ff0000>(P)</FONT></TD>
<TD align=center><FONT size=3>
	<INPUT name=P size=10> </FONT></TD>
</TR>

<TR>
<TD align=center><FONT face="Arial" size=2>Pitch Diameter:</FONT></TD>
<TD align=center><FONT color=#ff0000 size=1>(PD)</FONT></TD>
<TD align=center><FONT size=2>
	<INPUT name=PD size=10> </FONT></TD>
</TR>

<TR>
<TD align=center><FONT face="Arial" size=2>Best Wire:</FONT></TD>
<TD align=center><FONT size=1 color=#ff0000>(W)</FONT></TD>
<TD align=center><FONT size=3>
	<INPUT name=W size=10> </FONT></TD>
</TR>

<TR>
<TD align=center><FONT face="Arial" size=2>3 Wire Distance:</FONT></TD>
<TD align=center><FONT size=1 color=#ff0000>(M)</FONT></TD>
<TD align=center><FONT size=2>
	<INPUT name=M size=10> </FONT></TD>
</TR>

</TABLE>

</FORM>

</CENTER>

</BODY>
</HTML>

What am I missing?
Any ideas on fixing this?
Thanks for any help you all can give.


Oak
 
You can use parseInt(variable_name) to convert strings that won't add into integers that will.

--Glen :)

Memoria mihi benigna erit qui eam perscribam
 
I think your problem lies here:
Code:
f.M.value = eval (f.OD.value + (3 * W) - (1.5155 / [COLOR=red][b]f.[/b][/color]NT[COLOR=red][b].value[/b][/color]) );

Also, you could probably do without all the eval functions, they aren't needed for what you're trying to do. Additionally, eval has a lot of overhead and is a very timely function, try to avoid it whenever possible.


On a side note, unless you're referencing the song from The Jungle Book, you probably meant to say the bare necessities. [smile]

-kaht

...looks like you don't have a job, so why don't you get out there and feed Tina.
headbang.gif
[rockband]
headbang.gif
 
stormbind, thanks for the suggestion but I tried everything concerning ParseInt and all I got in return was NaN.

kaht, you were right about the code. It was wrong when I posted the question but I had already fixed it, but it did not solve the problem. You did make a good catch on the eval. They were removed.

Solution to problem:
Problem expression: f.M.value = ( (f.OD.value) + (3 * f.W.value ) - (1.5155 / f.NT.value) ).toFixed(5);

Fix: f.M.value = ( (f.OD.value) - (3 * f.W.value * -1) - (1.5155 / f.NT.value) ).toFixed(5);

I have no idea why the expression will not add, and believe me I have narrowed the whole expression down piece by piece and the problem DOES lie in the addition.
So, I subtracted the value after multiplying it by –1, thus the same as adding…success.

By the way kaht; Yes, I meant “bear”, don’t you know, it’s a jungle out there ;>)

Thanks for the suggestions guys.

"and it only gets worse"

Oak
 
The reason that it works when subtracting and not adding is because you cannot subtract 2 strings. When you try, javascript will automatically cast the strings as numbers if applicable. For instance, try this:
Code:
<script language=javascript>
alert("8" [COLOR=red][b]-[/b][/color] 3);
</script>
You are subtracting 3 from the string "8", however 8 will be cast to an integer and the result will be 5.

Now, when adding it is a different story. When you try to add a string to a number, it will instead perform string concatenation instead of adding the numbers together. Here's the same example:
Code:
<script language=javascript>
alert("8" [COLOR=red][b]+[/b][/color] 3);
</script>
Instead of displaying 11, you get 83. By default javascript will concatenate the number 3 to the end of the string "8".

Now, using stormbind's suggestion above, we can use parseInt to change this string to an integer before the addition takes place. After the string is cast to an integer, we will be adding to integers together and no string concatenation will take place:
Code:
<script language=javascript>
alert([COLOR=red][b]parseInt([/b][/color]"8"[COLOR=red][b], 10)[/b][/color] + 3);
</script>

The 2nd parameter passed to the parseInt function (10) is to ensure that the string is converted into a decimal integer. Strings that start with a "0" (try this instead to see for yourself: parseInt("08")) are converted to octal numbers by default so it is good practice to always explicitly define them to be decimal.

And one last thing to wrap it up, I wasn't complete in my first suggestion, I forgot to define the form and .value for the W variable above as well, but it seems you've already got it working :)

-kaht

...looks like you don't have a job, so why don't you get out there and feed Tina.
headbang.gif
[rockband]
headbang.gif
 

I agree with everything you and stormbind say, absolutely, no problem, but parseInt is applied to an existing integer…the number 8. Then has 3 added to it, thus 11.

I don’t see how you can apply parseInt to an expression. My values do not exist until the “Calculate” button is clicked. The value only exist in an expression; f.M.value = (f.OD.value + 3); It can not be expressed as alert(parseInt("f.OD.value", 10) +3); because f.OD.value is a non-integer and will return NaN by definition of parseInt. I’m afraid if I went to all the trouble of coding f.OD.value to get its integer so parseInt could be applied, it would be more trouble than my multiply by –1 and subtract.

If I was able to get the integer of f.OD.value then I could add, but I don’t see how.

If you can do this I would really be interested in seeing it.


Oak
 
I think I understand now.... The value in f.OD.value would be something alone the lines of: "8 - 3 + 7", I was assuming the user was just entering a number into that field instead of an expression. That makes more sense now. In this instance, I guess eval could be a viable and justified solution, however there are easier ways to "hack" your way around it. For instance, instead of multiplying by -1 and then subtracting the values, why not instead cast the expressions to an integer by subtracting 0 from them as soon as they're pulled? I guess it's not the prettiest solution, but it is arguably better than changing the arithmetic structure of your algorithm. Something like this:

Code:
 f.M.value = ( (f.OD.value - 0) + (3 * (f.W.value - 0)) - (1.5155 / (f.NT.value - 0)) ).toFixed(5)

And if that doesn't work, I'd probably stick to eval:

Code:
 f.M.value = ( eval(f.OD.value) + (3 * eval(f.W.value) ) - (1.5155 / eval(f.NT.value)) ).toFixed(5)

-kaht

...looks like you don't have a job, so why don't you get out there and feed Tina.
headbang.gif
[rockband]
headbang.gif
 
By jove I think you got it kaht.

Both methods work, but I think I'll go with the eval route. That way when someone else comes along and tries to figure out what I was doing, it might be easier to understand. I think the –0 and multiplying by -1 and then subtracting could lead someone to wonder what the true intention is.

For this you get a great big *.

Thanks for hanging in there with me and coming up with an acceptable solution.



Oak
 
Glad I could help.

-kaht

...looks like you don't have a job, so why don't you get out there and feed Tina.
headbang.gif
[rockband]
headbang.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top