How can i make 2 different shuffled arrays from an initial array in javascript?
When i execute my code the 2 shuffled arrays are always the same. I want my initial array to be unchangeable and the 2 new arrays to be differently shuffled.
Thank you!
const array = [
{
name: "Nick",
age: 15,
grade: 18
},
{
name: "John",
age: 16,
grade: 15
},
{
name: "Patrick",
age: 13,
grade: 11
},
{
name: "George",
age: 15,
grade: 19
}
];
console.log(array);
function shuffle(array) {
let currentIndex = array.length, randomIndex;
while (currentIndex != 0) {
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex--;
[array[currentIndex], array[randomIndex]] = [
array[randomIndex], array[currentIndex]];
}
return array;
}
let array1 = shuffle(array);
console.log(array1);
let array2 = shuffle(array);
console.log(array2);