by Pawan Kumar @jsartisan
Implement a shuffle
function that takes an array as input and returns a new array with its elements shuffled in random order. The original array should remain unmodified.
Requirements:
- The function should be named
shuffle
. - The function should take one argument:
- An array of elements to be shuffled.
- The function should return a new array with the elements shuffled in random order.
Example Usage:
const arr = [1, 2, 3, 4, 5];
const shuffledArr = shuffle(arr);
console.log(shuffledArr); // [3, 5, 1, 4, 2] (example, actual result will vary)
console.log(arr); // [1, 2, 3, 4, 5] (original array remains unchanged)