Can't sum variables

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • londres9b
    New Member
    • Apr 2010
    • 106

    Can't sum variables

    I have this code which retrives the anchor value from the url.
    (www.example.com/page#topic1 > gives 'topic1' )

    Code:
    var anchorValue;
    var url = document.location;
    var strippedUrl = url.toString().split("#");
    if(strippedUrl.length > 1)
    anchorvalue = strippedUrl[1];
    I want to do a very simple thing, add, for example '10' to the anchorvalue variable.

    So if we have www.example.com/page#20 the anchorvalue gives '20' and I want to increment that value in '10' so that I get '30'.


    Code:
    var anval = anchorvalue;
    var sumvalue = [B]10[/B];
    var newanchorvalue = anval + sumvalue;
    But that gives 2010 and not 30!

    How can I achieve what I want ?

    Thanks in advance.
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    I have this code which retrives the anchor value from the url.
    Code:
    window.location.hash
    ?

    But that gives 2010 and not 30!

    How can I achieve what I want ?
    that gives 2010 because the + operator performes both addition and string concatenation. if the first operand is a string, it will do concatenation, if it is a number, it will do addition. thus you have to cast your variable to a number, of which different ways are possible:
    - use the Number constructor Number(val)
    - use a mathematical operation val * 1 or val - 0
    - use a conversion function parseInt()/parseFloat()

    Comment

    • londres9b
      New Member
      • Apr 2010
      • 106

      #3
      window.location .hash gives #anchor and I want just anchor ...

      I know why it gives 2010 but as I know very little of Javascript, I can't make anchorvalue a number.

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        window.location .hash gives #anchor and I want just anchor ...
        I assume, you are able to remove the first character, ain’t you?

        I can't make anchorvalue a number.
        I told you how to do that

        Comment

        • londres9b
          New Member
          • Apr 2010
          • 106

          #5
          I got it, I multiplied the var by 1, like you suggested.

          Thank you very much :D

          Here's the code:

          Code:
          var anchorValue;
          var url = document.location;
          var strippedUrl = url.toString().split("#");
          if(strippedUrl.length > 1)
          anchorvalue = strippedUrl[1];
          
          [B]var anchorNumber = anchorvalue * 1;
           
          var increment = 10;
          
          var result = anchorNumber+increment;
          [/B]

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            you could also have switched increment and anchorNumber, since increment is already a number.

            PS.
            Code:
            var anchorValue = window.location.hash.substr(1);
            Last edited by Dormilich; Aug 18 '10, 01:21 PM.

            Comment

            • londres9b
              New Member
              • Apr 2010
              • 106

              #7
              I assume, you are able to remove the first character, ain’t you?
              Why? I know very little of JavaScript..

              Code:
              var anchorNumber = window.location.hash.substr(1);
              Thanks :)

              you could also have switched increment and anchorNumber, since increment is already a number.
              Like this right?
              Code:
              var anchorNumber = anchorvalue * 1;
              var result = anchorNumber+10;

              Comment

              • Dormilich
                Recognized Expert Expert
                • Aug 2008
                • 8694

                #8
                Like this right?
                nope. (remember what I said about the type of the first operand)

                Code:
                var result = 10 + anchorNumber;

                Comment

                • londres9b
                  New Member
                  • Apr 2010
                  • 106

                  #9
                  Ok,

                  Many Thanks.

                  Comment

                  Working...