如何从其他单词列表中随机显示一个单词? [英] How to display a word randomly from list of other words?
本文介绍了如何从其他单词列表中随机显示一个单词?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
$ b
var names = [Bob,Aaron,约翰];
我将如何获得一行或多行代码,可以随机在这三个名称之间进行选择显示它?我会用math.random()吗?
解决方案
因为你有一个名称集合而不是键值对,所以最好使用一个数组来存储你的名字而不是散列。
使用数组,您可以使用Math.floor和Math.random生成索引以在names数组中查找。
names [Math.floor(Math.random()* names.length)]
Ok so i have an array of names like this
var names = ["Bob", "Aaron", "John"];
How would i have a line or lines of code that would randomly pick between all 3 of these names and display it? Would i use math.random()?
解决方案
Since you have a collection of names rather than key value pairs it's better to use an array to store your names rather than a hash.
With an array, you can use Math.floor and Math.random to generate an index to look up in the names array.
names[Math.floor(Math.random() * names.length)]
这篇关于如何从其他单词列表中随机显示一个单词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文