Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Jan 2006
    Posts
    243
    Thanks
    14
    Thanked 2 Times in 2 Posts

    getting individual numbers from variable?

    Hi, I'm having problems when trying to retrieve the individual numbers from a two number variable that is used in a function. Variable "Nr" is a two number value, I want to get the first number and declare it as row and the second number as col. I have placed the bit that is causing problems below.

    Code:
    	
    function setColor( Nr )
    {
    	nrString = "" + Nr; // I use this to convert Nr to a string, it works but what is common practise?
    	row = nrString.getCharAt( 1 );
    	col = nrString.getCharAt( 2 );
    	alert( col );
    }
    What is going wrong here?

  • #2
    Regular Coder
    Join Date
    Jan 2003
    Posts
    867
    Thanks
    4
    Thanked 8 Times in 8 Posts
    Code:
    function setColor( Nr )
    {
    	nrString = "" + Nr; // I use this to convert Nr to a string, it works but what is common practise?
    	row = nrString.charAt( 0 );
            col = nrString.charAt( 1 );
    	alert( row );
    	alert( col );
    }

  • #3
    Regular Coder
    Join Date
    Jan 2006
    Posts
    243
    Thanks
    14
    Thanked 2 Times in 2 Posts
    great, thanks!

    Why is this, why doesn't it work like anywhere else in a program?

  • #4
    Regular Coder
    Join Date
    Jan 2003
    Posts
    867
    Thanks
    4
    Thanked 8 Times in 8 Posts
    In javascript, the function is charAt() and the index starts at 0.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •