|
-
Dec 19th, 2001, 12:02 PM
#1
Thread Starter
Lively Member
Easy question from a JS newbie
Is there an easier way to add the value of two strings converted to numbers rather than making one of them negative and subtracting it from the other?
Why won't this work?:
<HTML>
<SCRIPT LANGUAGE=javascript>
function setNewPosition()
{
var the_top= document.all.SPAN1.style.top;
var the_height=document.all.SPAN1.style.height;
var new_top = 0;
var new_height = 0
new_top = the_top.substr(0,the_top.length-2).valueOf();
new_height = the_height.substr(0,the_height.length-2).valueOf();
var new_position = new_height.valueOf() + new_top.valueOf();
alert(new_position);
}
</SCRIPT>
<BODY>
<SPAN ID=SPAN1 style="TOP: 100px; background-color: yellow; LEFT: 75px; POSITION: absolute; HEIGHT: 30px;">The Span</SPAN>
<input type=button value="click" onclick="setNewPosition();">
</BODY>
</HTML>
Where this will:
<HTML>
<SCRIPT LANGUAGE=javascript>
function setNewPosition()
{
var the_top= document.all.SPAN1.style.top;
var the_height=document.all.SPAN1.style.height;
var new_top = 0;
var new_height = 0
new_top = the_top.substr(0,the_top.length-2).valueOf();
new_height = the_height.substr(0,the_height.length-2).valueOf();
var new_position = new_height.valueOf() - -new_top.valueOf();
alert(new_position);
}
</SCRIPT>
<BODY>
<SPAN ID=SPAN1 style="TOP: 100px; background-color: yellow; LEFT: 75px; POSITION: absolute; HEIGHT: 30px;">The Span</SPAN>
<input type=button value="click" onclick="setNewPosition();">
</BODY>
</HTML>
-
Dec 19th, 2001, 12:19 PM
#2
Frenzied Member
1) Use vBulletin code tags.
2) Strip down your problem to the simpliest form, and then ask your question. I'm not wading through all that code because there is an addition problem somewhere in it. Be specific and brief.
Travis, Kung Foo Journeyman
As always, RTFM.
WWW Standards: HTML 4.01, CSS Level 2, ECMA 262 Bindings to DOM Level 1, JavaScript 1.3 Guide and Reference
Perl: Learn Perl, Llama, Camel, Cookbook, Perl Monks, Perl Mongers, O'Reilly's Perl.com, ActiveState, CPAN, TPJ, and use Perl;
YBMS, but Mozilla doesn't.
-
Dec 19th, 2001, 03:20 PM
#3
Thread Starter
Lively Member
How's this?
if you convert two strings to a number and try to add them together why does string1.valueOf() + string2.valueOf() not equal the sum of the two "values" but rather concatenates them. If I write string1.valueOf() - - string2.valueOf() it adds them together. My basic question is why doesn't "+" simply add the two values together? Is there something else I need to do to convert the strings into numbers besides .valueOf()?
-
Dec 19th, 2001, 03:33 PM
#4
Frenzied Member
*shrug*
RTM
What about:
Code:
var result = ToNumber(string1) + ToNumber(string2);
I haven't tested that, but the doco makes me think it should work. Becareful of NaN, though. It might break the script.
Travis, Kung Foo Journeyman
As always, RTFM.
WWW Standards: HTML 4.01, CSS Level 2, ECMA 262 Bindings to DOM Level 1, JavaScript 1.3 Guide and Reference
Perl: Learn Perl, Llama, Camel, Cookbook, Perl Monks, Perl Mongers, O'Reilly's Perl.com, ActiveState, CPAN, TPJ, and use Perl;
YBMS, but Mozilla doesn't.
-
Dec 20th, 2001, 11:23 AM
#5
Lively Member
run the strings through the parseInt function
-
Dec 20th, 2001, 11:54 AM
#6
Frenzied Member
Hmmm... yeap. It is official. The people at ECMA smoke crack.
parseInt() is probably want you want, since I am under the impression it will not die as easily as ToNumber() or ToInteger().
The elicit drug use comes into play when you see that parseInt() is not mentioned at all in the "Type Conversion" section of the 262 specs.
Travis, Kung Foo Journeyman
As always, RTFM.
WWW Standards: HTML 4.01, CSS Level 2, ECMA 262 Bindings to DOM Level 1, JavaScript 1.3 Guide and Reference
Perl: Learn Perl, Llama, Camel, Cookbook, Perl Monks, Perl Mongers, O'Reilly's Perl.com, ActiveState, CPAN, TPJ, and use Perl;
YBMS, but Mozilla doesn't.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|