...

View Full Version : getting individual numbers from variable?



Kirl
07-06-2006, 09:16 PM
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.



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?

arnyinc
07-06-2006, 09:20 PM
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 );
}

Kirl
07-06-2006, 09:41 PM
great, thanks!

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

arnyinc
07-06-2006, 09:44 PM
In javascript, the function is charAt() and the index starts at 0.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum