lundi 30 décembre 2019

Random Positions of an Array

I have a question. Attached is a section for my code needed for this question. I am shuffling the y position for 5 rectangles. (drag1, drag2, drag3, drag4, and drag5). The Y position shuffles and so do the rectangles but some of them land in the same spot on top of each other. Am I missing something? I need the for loop to start at 1.

var self=this;

dragPos = ["0","0","0","0","0","0"];//These arrays help in the rest of my code. They both ignore the 0 position.

correctPos = ["0","1","2","3","4","5"];//These arrays help in the rest of my code. They both ignore the 0 position.

yPos = [];

function shuffle(array){
   counter = array.length;

   while (counter > 0) {

      index = Math.floor(Math.random() * counter);

      counter--;

      temp = array[counter];

      array[counter] = array[index];

      array[index] = temp;

   }

   return array;

}

function positionAnswers() {

   for(e=1; e < dragPos.length; e++){

      yPos.push(self["drag" + e].y);

      shuffle(yPos);

      for(h=0; h < yPos.length; h++){

         self["drag" + e].y = yPos[h];

      }

   }

}

positionAnswers();



Aucun commentaire:

Enregistrer un commentaire