There are really two answers here.
The loop never exits because Array.indexOf()
returns -1
if the argument is not found, and -1 is true in a boolean context. The only way your loop will ever exit is if i
happens to be equal to _usedQuestions[0]
.
It may not be obvious, but even if you fix the above problem your loop will still fail to exit once all the questions have been used... and that's your real problem - you're using a confusing algorithm to do something simple.
It would make a lot more sense to simply keep two arrays - one of unseen questions and one of seen questions. Every time you choose a new question, simply remove it from unseen
and add it to seen
. Like this:
if (unseen.length > 0) {
var i:int = Math.floor( Math.random() * unseen.length );
seen.push( unseen[i] );
unseen.splice(i, 1);
} else // all questions seen...
Remember: writing programs that work correctly is merely the minimum requirement of programming. A good programmer writes programs that are easily understood by people as well! And a big part of that is making sure that simple things are done in simple ways. (Unless performance is an inescapable factor - but in this case, I absolutely promise that it won't!)