I am trying to write code to select n random elements from an array. For example:
const originalArray = ["a", "b", "c", "d", "e", "f", "g", "h", "i"];
const randomSelection = (n) => {
if (n > originalArray.length) {
return originalArray;
} else {
//Some Code to randomly select n elements from orignialArray
}
}
const newArray = randomSelection(2); //Random selection of 2 elements from the above array
What would be the code to achieve this? I searched for case examples but wondered if there is a simpler and more straightforward way to do this.
Any advice? Thanks a lot in advance!