Wednesday, November 2, 2016

javascript suffle


function shuffle(array) {
  var m = array.length, t, i;
     while (m) {
         i = Math.floor(Math.random() * m--);
         t = array[m];
         array[m] = array[i];
         array[i] = t;
     }

     return array;
}

// test call

var letter= ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]

var random = shuffle(letter.slice());
console.log(random)
console.log(letter)
shuffle(letter)
console.log(letter)

See the Pen oYvpYw by Usman Fauzi (@flint002) on CodePen.

No comments: