+ 1
How to tell JavaScript to make a choice between some given choices.
I want to tell JavaScript to make a choice regarding some options how I can do so.
3 Answers
+ 8
Are you talking about random choices?
+ 7
You can do this:
var options = ["c1","c2","c3","c4"] ;
function makeChoice(arr) {
return
Math.floor(
Math.random()*arr.length
);
}
console.log(
options[
makeChoice(options)
]
);
+ 7
function selectFrom(lower, upper) {
let choices = upper - lower + 1;
return Math.floor(Math.random() * choices + lower)
}
let fruits = ["apple", "kiwi", "orange", "banana", "mango", "apricot", "peach"];
fruits[selectFrom(0, fruits.length - 1)]