Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript [1,2,3,4,5,6,7,8,9].shuffle() Re: [1,2,3,4,5,6,7,8,9].shuffle()

#143833
Kuzyo
Participant

Give me, please some tips, here my result (don’t want to look at Stock overflow, I know there is answer, want to properly uderstand):

Array.prototype.shuffle = function() {
var len = this.length,
shuffleArr = [];

for ( var i = 0; i < len; i++ ) {
var elem = this,
pos = Math.floor( Math.random() * len );

if ( shuffleArr[pos] == undefined) {
shuffleArr[pos] = elem
shuffleArr.push(elem);
}

}

return shuffleArr;
}

var a =[1,2,3,4,5,6,7,8,9];

a.shuffle()