Results 1 to 6 of 6

Thread: Easy question from a JS newbie

  1. #1

    Thread Starter
    Lively Member Nator's Avatar
    Join Date
    Nov 1999
    Location
    East Larryville, GA
    Posts
    80

    Question 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>

  2. #2
    Frenzied Member
    Join Date
    Feb 2001
    Posts
    1,140
    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.

  3. #3

    Thread Starter
    Lively Member Nator's Avatar
    Join Date
    Nov 1999
    Location
    East Larryville, GA
    Posts
    80
    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()?

  4. #4
    Frenzied Member
    Join Date
    Feb 2001
    Posts
    1,140
    *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.

  5. #5
    Lively Member
    Join Date
    Mar 2001
    Location
    Massachusetts, USA
    Posts
    111
    run the strings through the parseInt function

  6. #6
    Frenzied Member
    Join Date
    Feb 2001
    Posts
    1,140
    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
  •  



Click Here to Expand Forum to Full Width