Posted By

chrisaiv on 02/21/07


Tagged

actionscript flash


Versions (?)

AS1: Swap Numbers


 / Published in: ActionScript
 

Needed a way to mix up the order of questions and answers for a Flash quiz.

  1. MovieClip.prototype.swapNumbers = function(numOfDigits:Number) {
  2. //1. Construct a temporary array
  3. var newOrder:Array = new Array();
  4.  
  5. //2. Build a condition where the array must have a specified number of digits
  6. while (newOrder.length != numOfDigits) {
  7. var skipNum:Boolean = false;
  8. //a. Generate a random number
  9. var randNum:Number = Math.ceil(Math.random() * numOfDigits);
  10.  
  11. trace("Random Number Picked: " + randNum);
  12.  
  13. //b. Run through the array.length and make sure there are no doubles
  14. for (var p:Number = 0; p < newOrder.length; p++){
  15. if (newOrder[p] == randNum)
  16. {
  17. skipNum = true;
  18. }
  19. }
  20.  
  21. //c. If the randNum is unique to the array, then push it into the array
  22. if (skipNum == false)
  23. {
  24. newOrder.push(randNum);
  25. }
  26. }
  27. //3. Once the array has the right mount of swapped digits, return the array
  28. return newOrder;
  29. };
  30.  
  31. var quizOrder:Array = this.swapNumbers(12);
  32. trace(quizOrder);

Report this snippet  

You need to login to post a comment.