russ_man7
06-24-2003, 02:35 AM
This loop insists on replacing the first element of the array each time through, instead of making an array with five elements counting 1 to 5. I would think, since the length of an array is always one bigger than the last element number, that each time through the length would be one bigger, and thereby avoiding replacing any previous elements. Instead, it keeps putting the most recent loop number in the first location, so when it prints it says the only element is 5 and the length is 1.
I tried putting a "list_items.length++", and later "list_items.length = i+1", both before and after the assignment to the array, but all these refused to talk to any location other than the first one. They did, however, add enough elements to bring the length up to where it would be expected to be, but all the extra elements were "undefined."
var list_items = new Array;
function addItem()
{
for (i=0; i<6; i++)
{
list_items[length] = i;
}
alert(list_items + ", " + list_items.length);
}
If anyone has any deep insights, or just another stupid thing i missed in my pre-dawn stupor, please share.
Russell
I tried putting a "list_items.length++", and later "list_items.length = i+1", both before and after the assignment to the array, but all these refused to talk to any location other than the first one. They did, however, add enough elements to bring the length up to where it would be expected to be, but all the extra elements were "undefined."
var list_items = new Array;
function addItem()
{
for (i=0; i<6; i++)
{
list_items[length] = i;
}
alert(list_items + ", " + list_items.length);
}
If anyone has any deep insights, or just another stupid thing i missed in my pre-dawn stupor, please share.
Russell