Implement Array.shuffle (Medium)
Good evening! 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.

xxxxxxxxxx25
// 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 arrayshuffle(arr);// Validate the shuffled arrayconst 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
Results will appear here.