/    Sign up×
Community /Pin to ProfileBookmark

converting int to string

Hi,

Am I doing something wrong with this code?

I have a for loop:

[CODE]
for( var i = 0; i < aInput.length; i++ )
[/CODE]

I want to use this i variable to concatonate it as a string to find an input box

[CODE]
var j = i;
var qualname = “discountqualifier” + j;
qualname.toString();

if ( inputName == ( qualname ) )
{
[/CODE]

Assuming I have a input box named discountqualifier0, discountqualifier1, discountqualifier2 etc…

Should this match or am I missing something?

Thanks,
Joe

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@shifter480authorJun 26.2010 — Just wanted to correct that if statement as it now makes more sense rather than bracketing out the logic:

NOW

if ( inputName == qualname )
Copy linkTweet thisAlerts:
@Declan1991Jun 26.2010 — No need for "qualname.toString". It's a string already.
Copy linkTweet thisAlerts:
@shifter480authorJun 26.2010 — The "discountqualifier" is a string but the j OR i is an int as I am using it in the for loop...

If I do this:

var qualname = "discountqualifier1";

then it will do as expected but

"discountqualifier" + j

will not???

Any ideas?

Thanks
Copy linkTweet thisAlerts:
@Declan1991Jun 26.2010 — Yes it will, unless you have a mistake elsewhere.
var qualname = "discountqualifier1";
var qualname = "discountqualifier" + 1;
var qualname = "discountqualifier" + "1";
They are all functionally equivalent. Only once do you have to be careful, when you want two numbers to become strings. Other than that, they will be automatically cast by the concatenation.
×

Success!

Help @shifter480 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...