+ 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.

16th Mar 2019, 7:33 AM
Pooja Tiwari
3 Respostas
+ 8
Are you talking about random choices?
16th Mar 2019, 7:40 AM
Hatsy Rei
Hatsy Rei - avatar
+ 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) ] );
16th Mar 2019, 9:06 AM
VEDANG
VEDANG - avatar
+ 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)]
16th Mar 2019, 9:27 AM
Вап