Javascript shuffle function returns same value in for loop -


Why do I get three arrays in the same order while I recite them in a shuffle ceremony in the loop?

  var items = ['x', 'y', 'z', 'a', 'b']; Var createSlots = function (slots) {slots = slots || 3; Var slot rack = []; (Var i = 0; i & lt; slot; i ++) {slotRack.push (shuffle (item)); } Return slot rack; } Function for shuffle (o) {// v1.0 (var j, x, i = o.length; i; j = math.flur (monastery.rendum) * i), x = o [- i] , O [ii] = o [ja], o [j] = x); Return O; }; Var SlotMatchin = Create Slot (); // Returns the three array with values ​​in the same order ... do not want to ... :( console.log); = "Post-Text" itemprop = "text"> 

squint mentioned your problem in a comment given above.

Anyway, This is a cooler shuffle method that will always get you out of trouble:

  function shuffle (ARR) {return arr.sort (function () {return Math.random () - Math.random ( }})}}  

edit (thanks to Mr. Use ):

( Christoph for implementation):

  Function Shuffle (array) {Var tmp, current, top = array.label; if (top) while (- top) {current = Math.floor (Math.random () * (top + 1)); tmp = array [current]; array [ Current] = array [top]; array [top] = tmp;} return array;}  

Comments

Popular posts from this blog

java - Can't add JTree to JPanel of a JInternalFrame -

javascript - data.match(var) not working it seems -

javascript - How can I pause a jQuery .each() loop, while waiting for user input? -