Here is a pretty weird problem for me.
I have an array, like this:
let rolescheck1 = [1, 2, 3, 4]
And then I want to randomly select an element from that Array, then I will remove it from that Array until there is no element left inside that Array:
let randomroles = Math.floor(Math.random() * rolescheck1.length);
console.log(rolescheck1[randomroles]);
var selectedrole = rolescheck1[randomroles];
rolescheck1 = rolescheck1.filter(function(item){
return item !== selectedrole
})
I put the randomly select part inside a for() function like this:
for(var i = 0; i <= rolescheck1.length; i++){
//The code above
}
But then it returned: 1, 2, 3, 2
I tried [4, 3, 2, 1] it still returned 1, 2, 3, 2
Anyone have ideas about what's going on? Thank you for your help!
Here is the full code:
for (var i = 0; i <= rolescheck2; i++) {
let randomroles = Math.floor(Math.random() * rolescheck1.length);
console.log(rolescheck1[randomroles]);
var selectedrole = rolescheck1[randomroles];
rolescheck1 = rolescheck1.filter(function(item){
return item !== selectedrole
})
}