/    Sign up×
Community /Pin to ProfileBookmark

Help-a-newbie with variables …

Greetings Javascripters,

Need to build a series of variables on the fly:

for(var i=1;i<=24;i++){
‘xxx’+i = ….
}

This doesn’t work.
Please advise.

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@pyroMar 10.2003 — Try this:

for(i=1;i<=24;i++)

{

xxx[i] = ....

}
Copy linkTweet thisAlerts:
@AdamBrillMar 10.2003 — One thing is you must create the array first. Like this:

xxx = new Array();

for(i=1;i<=24;i++)

{

xxx[i] = ....

}



Then it should work. I hope that helps... ?
Copy linkTweet thisAlerts:
@NedalsMar 10.2003 — for(var i=1;i&lt;=24;i++){
'xxx'+i = ....
}

A few points on your snippet of code.

You are not really creating [i]variables on the fly[/i]. You are attempting to use an array and it's a good idea to declare the array

var xxx = new Array(); ( ;'s are optional in javascript but with other languages they are not. So get into the habit of including them.)

Note that the first item in the array is always 'xxx[0]' so the 'for' loop is better written..
var xxx = new Array();
for (var i=0; i&lt;24; i++) {
xxx[i] = ....;
}


And that my mini tutorial for today! ?
×

Success!

Help @w_conti 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,
)...