+ 1
How to shuffle an array in JS ?
Does method exist to shuffle the elements in an array ?
3 Answers
+ 3
Thanks Calvin !
+ 1
Thank you Brent.
I find a solution of this problem in stackoverflow.com :
/* shuffle method -> https://stackoverflow.com/questions/6274339/how-can-i-shuffle-an-array */
shuffle() {
let j, x, i;
let a = array;
for (i = a.length-1; i>0; i--) {
const j = Math.floor(Math.random()*(i+1));
x = a[i];
a[i] = a[j];
a[j] = x;
}
return a;
}
https://code.sololearn.com/W0NV25NZ0TLp/?ref=app