Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

filling array value where part of value is always constant

Status
Not open for further replies.

llamatron

MIS
Aug 29, 2003
10
GB
I need to fill up an array so that it reads user1, user2, user3 etc.

My code looks like this :

<script>
var myname = new Array(11);
for (i=0; i<11;i++){
myname=user+;
}

</script>

I keep getting a javascript error. Why is this syntax wrong?
Thanks,
 
aaagh course last line should read:

myname=user+;
}
</script>

Please help....
 
why is it leaving square brackets out,
this forum is as user friendly as javascript
 
<script>

var myname = new Array(11);

for (i=0; i<11;i++){
myname = &quot;user&quot; + i;
}

</script>
 
yeah, strange it's leaving them out, and everything is getting italicized as well

anyway, just encapsulate the constant part in quotes

-kaht
 
Use [ignore]
Code:
[/ignore] tags around your code to stop [ignore][/ignore] being interpreted as a directive to italicise your code.

Either that or use a variable name other than 'i' for subscripting your arrays.
 
Okay fair enough, but do you know how to solve my original problem?
 
You want an array of strings with incremental numbers appended to them?
Code:
var prefix = &quot;user&quot;;
var numElements = 11;
var startAt = 1;

var stringArray = new Array();

for(var count = 0; count < numElements; count++){
 stringArray[count] = prefix + startAt;
 startAt++;
}

alert(stringArray);
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top