People
My question is the following. I have two arrays, and I would like to move an element from array A to array B. However it is not so easy. Which element would go is totally random based. I copy my code so my goal would be easier to understand:
var array1 = ["A","B","C"];
var array2 = [];
var move = function() {
var x = Math.floor((Math.random() * 100) + 1);
if (x > 0 && x < 33.1 && array1[0] !== "undefined")
{array2.push(array1[0])
delete array1[0]
}
if (x > 33.2 && x < 66.1 && array1[1] !== "undefined")
{array2.push(array1[1])
delete array1[1]
}
if (x > 66.2 && x < 100.1 && array1[2] !== "undefined")
{array2.push(array1[2])
delete array1[2]
}
else {
move();
return
}
}
move();
move();
move();
console.log(array1);
console.log(array2);
So the objective is that the function called 3 Times, we would have the three elements in the B array. I would like my function to restart automatically if the random number is "undefined". So for the next random roll, it would pick another element. And so on so on. My idea in the code, but its not working, since it is still dropping "undefined" ones. Thanks,