+ 1

How to randomly pick strings in js

Random

23rd Mar 2019, 11:42 AM
SCP 251
SCP 251 - avatar
10 Respuestas
+ 9
Store in array & get a random value from that
23rd Mar 2019, 11:45 AM
Jaydeep Khatri
Jaydeep Khatri - avatar
+ 8
// You can do this: var options = ["s1","s2","s3","s4"] ; function makeChoice(arr) { return Math.floor( Math.random()*arr.length ); } console.log( options[ makeChoice(options) ] );
23rd Mar 2019, 2:23 PM
VEDANG
VEDANG - avatar
+ 5
¿ ? Arr = [1, 2, 3] console.log(Arr[Math.floor(Math.random() * Arr.length)])
23rd Mar 2019, 12:16 PM
Lord Krishna
Lord Krishna - avatar
+ 5
VEDANG Your code returns undefined. the value you are returning must be on the same line as the return statement. return Math.random() //Or use parenthesis return ( Math.floor( Math.random()*arr.length ) ); }
18th Apr 2019, 6:28 PM
Lord Krishna
Lord Krishna - avatar
+ 5
Lord Krishna Oh yes you are right now its corrected. I wasn't knowing that. Thanks for telling.
18th Apr 2019, 6:45 PM
VEDANG
VEDANG - avatar
+ 3
You can see my example code for random colors. Hope it helps you. https://code.sololearn.com/WlEL2L3JmSyl/?ref=app
23rd Mar 2019, 12:21 PM
TheWh¡teCat 🇧🇬
TheWh¡teCat 🇧🇬 - avatar
+ 2
https://code.sololearn.com/WYFcedEwfNHw/?ref=app
23rd Mar 2019, 12:52 PM
Calviղ
Calviղ - avatar
+ 1
How
23rd Mar 2019, 11:49 AM
SCP 251
SCP 251 - avatar
0
Example
23rd Mar 2019, 11:51 AM
SCP 251
SCP 251 - avatar
0
Thanks
23rd Mar 2019, 7:58 PM
SCP 251
SCP 251 - avatar