> Can you
> spot the error?
yes :)
why not simply do something like that:
while( myArray.length ) myArray.pop();
?
mark
On 11/21/05, Judah Frangipane <[EMAIL PROTECTED]> wrote:
> I just want to bring to your attention a rare array problem that eluded
> me for years. I just realized what is going on.
>
> Whenever I tried to clear an array I would use a for loop, iterate
> through all the items and pop each one off. Every once in a while the
> arrays would still contain values and not be completely erased. Can you
> spot the error? Here is the code:
>
> *****************************************
> wrong way
> *****************************************
> // create an array
> errors = new Array()
> // add two items
> errors.push("item 1")
> errors.push("item 2")
>
> // loop through each item
> for (var i=0;i < errors.length; i++) {
> trace("removing item")
> errors.pop()
> }
> // errors.length = 1
> trace("errors.length="+errors.length)
>
>
> *****************************************
> right way
> *****************************************
> // create an array
> errors = new Array()
> // add two items
> errors.push("item 1")
> errors.push("item 2")
> var len = errors.length;
>
> // loop through each item
> for (var i=0;i < len; i++) {
> trace("removing item")
> errors.pop()
> }
> // errors.length = 0
> trace("errors.length="+errors.length)
>
>
> Look at the condition (i < errors.length).
>
> If we pop an item off the end of the array then the length of the array
> is decreased and we do not iterate through all items in the array. Hope
> this helps someone.
>
> Best Regards,
> Judah
>
>
>
> _______________________________________________
> Flashcoders mailing list
> [email protected]
> http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
>
--
http://snafoo.org/
jabber: [EMAIL PROTECTED]
_______________________________________________
Flashcoders mailing list
[email protected]
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders