0

I have an array with list items :

var listArray = [];

$("ul li").each(function(){
    listArray.push($(this));
});

var item = listArray[Math.floor(Math.random()*listArray.length)];

item.css({
    "transform":"scale(1)"
});

and i shuffled the array as it mentioned in answers but still i am not able to pull the elements from it one by one in intervals

if you have a better idea how to do it please tell me.

DEMO : https://jsfiddle.net/rnfrxL1b/3/

2 Answers2

2

You may first shuffle the array and then pull values from the shuffled array either one by one or 4 by 4. See shuffle method: link

Community
  • 1
  • 1
imnancysun
  • 612
  • 8
  • 14
  • ok i shuffled array but the problem is that i dont know how to pull elements from it 4 by 4 in intervals i have tried it with loops but without success –  Oct 05 '15 at 18:47
  • @FrankeyWakey Did you try array slice? – imnancysun Oct 05 '15 at 20:52
0

I think this is what you want: How to randomize (shuffle) a JavaScript array?

Accepted/most upvoted answer:

function shuffle(array) {
  var currentIndex = array.length, temporaryValue, randomIndex ;

  // While there remain elements to shuffle...
  while (0 !== currentIndex) {

    // Pick a remaining element...
    randomIndex = Math.floor(Math.random() * currentIndex);
    currentIndex -= 1;

    // And swap it with the current element.
    temporaryValue = array[currentIndex];
    array[currentIndex] = array[randomIndex];
    array[randomIndex] = temporaryValue;
  }

  return array;
}

Which is the Fisher-Yates (aka Knuth) Shuffle, according to the author of the answer. Please see original answer and links in it for more details.

Community
  • 1
  • 1
Gui Imamura
  • 556
  • 9
  • 26