Good afternoon! Here's our prompt for today.
Shuffling an array is an operation that generates a random inline permutation of the elements of a given array. Can you implement a shuffle method, that will accept one parameter - the array to shuffle, and will shuffle its elements in a random order?
An example of how your method should work, would be to generate one of the possible 24 permutations for the array in our example.

Try to solve this here or in Interactive Mode.
How do I practice this challenge?
xxxxxxxxxx
25
// Shuffle function implementation - fill in your code!
function shuffle(arr) {
// fill this in
}
const arr = [1, 2, 3, 4];
const permutations = [
[1, 2, 3, 4], [1, 2, 4, 3], [1, 3, 2, 4], [1, 3, 4, 2], [1, 4, 2, 3], [1, 4, 3, 2],
[2, 1, 3, 4], [2, 1, 4, 3], [2, 3, 1, 4], [2, 3, 4, 1], [2, 4, 1, 3], [2, 4, 3, 1],
[3, 1, 2, 4], [3, 1, 4, 2], [3, 2, 1, 4], [3, 2, 4, 1], [3, 4, 1, 2], [3, 4, 2, 1],
[4, 1, 2, 3], [4, 1, 3, 2], [4, 2, 1, 3], [4, 2, 3, 1], [4, 3, 1, 2], [4, 3, 2, 1]
];
// Shuffle the array
shuffle(arr);
// Validate the shuffled array
const isValidPermutation = permutations.some(permutation => JSON.stringify(permutation) === JSON.stringify(arr));
if (isValidPermutation) {
console.log('The shuffled array is a valid permutation:', arr);
} else {
console.log('Something went wrong! The shuffled array does not match any of the valid permutations.');
}
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment
Here's our guided, illustrated walk-through.
How do I use this guide?