Here's one method for using an if-loop to shuffle values from other arrays and create a new array:
let shuffledArray = [];
let numValuesToShuffle = 10;
for (let i = 0; i < numValuesToShuffle; i++) {
// code to shuffle values will go here
}
let randNum = Math.floor(Math.random() * 2); // generates a random number between 0 and 1
if (randNum === 0) {
let randomIndex = Math.floor(Math.random() * array1.length);
shuffledArray.push(array1[randomIndex]);
} else {
let randomIndex = Math.floor(Math.random() * array2.length);
shuffledArray.push(array2[randomIndex]);
}
Note: This method assumes that you have existing arrays from which you want to shuffle values. If you need to create new arrays with randomized values, you would need to modify this code accordingly. You could also modify the if-statement to shuffle values from more than two arrays.
Asked: 2021-08-02 11:00:00 +0000
Seen: 8 times
Last updated: Nov 24 '21