-1

I want the computer to randomly choose either 8 or 2. I tried doing (Math.random()*8)+2, it chooses a random number BETWEEN 8 and 2 but I want the computer to randomly choose 8 or 2.

So I tried a different way, and I did this:

if(b==0){
var a = 2;
}
else{
var a = 8;
}

However, it is not working. Is there a more efficient and better way?

Thank you, Paul

1

1 Answer 1

3

Something like this should do.

function randomSelection(values) {
  return values[Math.floor(Math.random() * values.length)];
}

numbers = [2,8];
randomSelection(numbers);

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.