Published in: ActionScript
Needed a way to mix up the order of questions and answers for a Flash quiz.
MovieClip.prototype.swapNumbers = function(numOfDigits:Number) { //1. Construct a temporary array var newOrder:Array = new Array(); //2. Build a condition where the array must have a specified number of digits while (newOrder.length != numOfDigits) { var skipNum:Boolean = false; //a. Generate a random number var randNum:Number = Math.ceil(Math.random() * numOfDigits); trace("Random Number Picked: " + randNum); //b. Run through the array.length and make sure there are no doubles for (var p:Number = 0; p < newOrder.length; p++){ if (newOrder[p] == randNum) { skipNum = true; } } //c. If the randNum is unique to the array, then push it into the array if (skipNum == false) { newOrder.push(randNum); } } //3. Once the array has the right mount of swapped digits, return the array return newOrder; }; var quizOrder:Array = this.swapNumbers(12); trace(quizOrder);
You need to login to post a comment.
