Shuffle array with while loop - Node.js Array

Node.js examples for Array:Shuffle

Description

Shuffle array with while loop

Demo Code


Array.prototype.shuffle = function() {
  var i = this.length, j, tempi, tempj;
  if ( i == 0 ) return false;
  while ( --i ) {
     j       = Math.floor( Math.random() * ( i + 1 ) );
     tempi   = this[i];//from w w w.j  ava  2  s .c  o  m
     tempj   = this[j];
     this[i] = tempj;
     this[j] = tempi;
  }
  return this;
}

Related Tutorials