Discussion

#158 shuffle

medium
javascript

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:

  1. The function should be named shuffle.
  2. The function should take one argument:
    • An array of elements to be shuffled.
  3. 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)