0

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
  })
} 

4
  • i<=Array is not the way to iterate an array Commented Jun 10, 2021 at 16:44
  • Can you update the question to include a runnable example demonstrating the problem? At a glance, i < rolescheck1 looks strange because rolescheck1 is an array. And modifying an array while you iterate it seems like a recipe for bugs. But for the overall functionality being described, you may be over-thinking it. Just shuffle the array. Commented Jun 10, 2021 at 16:44
  • Oh i meant array.length not array and sure things gimme a few sec Commented Jun 10, 2021 at 16:46
  • @Hán: The "full code" doesn't produce the output described. It does however produce a console error. Commented Jun 10, 2021 at 16:51

2 Answers 2

1

Maybe this can solve your problem.

var orginal = [1,2,3,4];
var copy = [1,2,3,4];

function getRandomElement(){
   var idx = Math.floor(Math.random()*copy.length);
   var element = copy[idx];
   copy.splice(idx,1);
   return element;
}


for(i = 0;i<orginal.length;i++){
 let element = getRandomElement();
}

The first array is your original data. If it is the second array, give a copy. If you delete an element from the copy data in each loop, you will always get a different value.

Sign up to request clarification or add additional context in comments.

2 Comments

Ok lemme give it a try!
OK. I'm waiting for the result.
0

This should work

let rolescheck1 = [1, 2, 3, 4];

let rolescheck2 = [...rolescheck1];

while (rolescheck2.length !== 0) {
   const randomroles = Math.floor(Math.random() * rolescheck1.length);
   const selectedrole = rolescheck1[randomroles];
   rolescheck2 = rolescheck2.filter((role) => role !== selectedrole);
}
console.log('rolescheck2', rolescheck2); // []

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.