I am writing a script where I have to randomly generate a number from an array and then remove the number so that it cannot be generated again. What appears to be happening is that the number generated, after being spliced, is removing other numbers from my array. I assume it is being subtracted. Here is the necessary code:
var randNum = [0,1,2,3,4,5,6,7,8,9];
function clickHandler ()
{
output = randNum[Math.floor(Math.random() * randNum.length)];
console.log("This is the generated number:" + output);
randNum.splice(output);
console.log("This is the resulting array without the generated number:" + randNum);
}
randNum = (n => randNum.filter(x => x !== n))(Math.floor(Math.random() * randNum.length));Trying to mutate the orignal array with.spliceis unnecessary and, as you've discovered, error prone.