How do run my code in JavaScript such that it makes a random choice from some given choices after the code is executed? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How do run my code in JavaScript such that it makes a random choice from some given choices after the code is executed?

I want the code to make a choice between some choices given randomly. How I can do it in JavaScript?

26th Apr 2020, 7:39 PM
saatwik
3 Answers
+ 3
var options = ['1st choice', '2nd choice', '3rd choice']; finalChoice = options[Math.floor(Math.random()*options.length)] https://code.sololearn.com/WNRBMpU2H6t0/?ref=app
26th Apr 2020, 8:43 PM
Ginfio
Ginfio - avatar
+ 1
Well, I want the output to be a number which is randomly chosen from the given few inputs.
26th Apr 2020, 8:35 PM
saatwik
- 2
Can you explain better what are you trying to accomplish - You make a function that create a random number whit Math. Random and set the desire chooses to that random number in the arra of options
26th Apr 2020, 7:55 PM
Ismael Garcia
Ismael Garcia - avatar