var array = [1, 2, 3, 4]
// [1, 2, 3, 4]
array.disorder()
// [2, 4, 1, 3]
array
// [2, 4, 1, 3]
var array = [1, 2, 3, 4]
// [1, 2, 3, 4]
array.disorder(true)
// [2, 4, 1, 3]
array
// [1, 2, 3, 4]
/** | |
* Disorder the array | |
* | |
* @param {bool} preserve Returns a copy without modifying the original | |
* @return {array} The disordered array | |
*/ | |
Array.prototype.disorder = function (preserve) { | |
var array = preserve ? this.slice() : this; | |
var disordered = []; | |
while(array.length > 0) { | |
var index = Math.round(Math.random()*(array.length-1)); | |
disordered.push(array[index]); | |
array.splice(index, 1); | |
} | |
if(!preserve) { | |
Array.prototype.push.apply(this, disordered); | |
} | |
return disordered; | |
}; |