The script:
let col1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
let col2 = [16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30]
for(let i = 0; i < 5; i++) {
const random = Math.floor(Math.random() * col1.length + 1)
const index = col1.indexOf(random)
const number = col1.at(index)
col1.splice(index, 1)
console.log(number)
}
for(let i = 0; i < 5; i++) {
const random = Math.floor(Math.random() * col2.length + 1)
const index = col2.indexOf(random)
const number = col2.at(index)
col2.splice(index, 1)
console.log(number)
}
The first for loop always generates 5 random numbers from the first array. But the second for loop always shows the same results (30, 29, 28, 27, 26). What’s causing this behavior?