+ 1
How to randomly pick strings in js
Random
10 Antworten
+ 9
Store in array & get a random value from that
+ 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)
]
);
+ 5
¿ ?
Arr = [1, 2, 3]
console.log(Arr[Math.floor(Math.random() * Arr.length)])
+ 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
)
);
}
+ 5
Lord Krishna Oh yes you are right now its corrected. I wasn't knowing that. Thanks for telling.
+ 3
You can see my example code for random colors. Hope it helps you.
https://code.sololearn.com/WlEL2L3JmSyl/?ref=app
+ 2
https://code.sololearn.com/WYFcedEwfNHw/?ref=app
+ 1
How
0
Example
0
Thanks