【发布时间】:2016-09-09 15:44:32
【问题描述】:
我有一个字符串数组,只需将其中的 4 个(随机)放入另一个数组中。
var a = ["Orange", "Red", "Yellow", "Blue", "Black", "White", "Brown", "Green"];
var b = [];
function selectColours(){
var toRandomise = a[Math.floor(Math.random() * 4)];
b.push(toRandomise);
}
console.log(b);
我的问题是控制台什么也没显示。
【问题讨论】:
-
你打电话给
selectColours()吗? -
这样你就只能得到“蓝色”了。将
a[Math.floor(Math.random() * 4)]替换为a[Math.floor(Math.random() * a.length)]
标签: javascript arrays function random