0

Is it possible to pick a random variable in JavaScript?

Selecting a random variable using a method or function?

30th Jun 2020, 2:03 PM
Express
Express - avatar
8 Réponses
+ 3
function random_item(items) { return items[Math.floor(Math.random()*items.length)]; } var items = [paul, Sarah, Peter]; console.log(random_item(items));
30th Jun 2020, 2:25 PM
Gulshat Taganova
Gulshat Taganova - avatar
+ 1
Yea of course!
30th Jun 2020, 2:10 PM
Gulshat Taganova
Gulshat Taganova - avatar
+ 1
Gulshat Taganova how? Can you write short code as an example or send a link to a page that tell me how to do it🙏
30th Jun 2020, 2:11 PM
Express
Express - avatar
+ 1
Gulshat Taganova I know about this, I meant a function that selects a variable you've set randomly, some thing like this: var nameOne = "paul"; var nameTwo = "Sarah"; var nameThree = "Peter"; If I want to select a random variable from the three variables how do I do it?
30th Jun 2020, 2:19 PM
Express
Express - avatar
30th Jun 2020, 2:31 PM
Raj Chhatrala
Raj Chhatrala - avatar
+ 1
Gulshat Taganova thanks a lot
30th Jun 2020, 3:31 PM
Express
Express - avatar
+ 1
Never mind
30th Jun 2020, 3:31 PM
Gulshat Taganova
Gulshat Taganova - avatar
0
Math.random(); Ex:Math.floor((Math.random() * 10) + 1); It returns random number between 1 and 10
30th Jun 2020, 2:15 PM
Gulshat Taganova
Gulshat Taganova - avatar