JavaScript array shuffle

preview_player
Показать описание
JavaScript array shuffle
That means to set elements of the array in random order.
Let we have an ordered array.
So let’s define the shuffle function.
The function takes one parameter - an array to shuffle.
We are gonna use the Fisher-Yates shuffle algorithm.
According to the algorithm for every item of the array from the last one to the first one
we select a random element from the beginning to the current element.
The current element is included.
And then we swap the current element and the randomly selected one.
We use array destructuring assignment to swap items.
That’s all.
The array has been shuffled.
Again.
And again.
Рекомендации по теме
visit shbcf.ru