How to shuffle an array in JS ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How to shuffle an array in JS ?

Does method exist to shuffle the elements in an array ?

27th Aug 2018, 7:42 AM
Frédéric Descloux
3 Answers
27th Aug 2018, 11:08 AM
Calviղ
Calviղ - avatar
+ 3
Thanks Calvin !
27th Aug 2018, 11:22 AM
Frédéric Descloux
+ 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
27th Aug 2018, 8:18 AM
Frédéric Descloux