As the following example suggests, we have a fruits array that consists of 4 elements and can be targeted by their index, starting from 0.
const fruits =
For example, we have the following array:
let arr = => [3, 5, 4, 1, 2] // a possible shuffle result
As the first example, we will define a function called randomize, which will take a parameter that is the array we want to shuffle. Then, we get a random index on each call and swap the elements' locations with each other, returning the values at the end.
Shuffling an array of values is considered one of the oldest problems in computer science. Shuffling is possible with the Fisher-Yates shuffle algorithm for generating a random permutation of a finite sequence. That is to say, and the algorithm shuffles the sequence.
We can implement the algorithm with a for loop compared to the first example.
Richard Durstenfeld introduces the modern version of the Fisher-Yates shuffle designed for computer use.