我有以下代码:JavaScript函数返回数组
var disArray = ['red','red','green','green','green','blue','blue','blue','blue','blue'];
var otherArray = [];
function takeOut() {
for (i = 0; i < 3; i++) {
var randItem = disArray[Math.floor(Math.random()*disArray.length)];
otherArray.push(randItem);
}
return otherArray;
}
takeOut(disArray)
console.log(otherArray)
我想在otherArray
返回的元素,当它被调用的函数,但我得到的错误undefined
。它只适用于我console.log
otherArray
。有什么办法可以让我的函数返回数组而不使用console.log
?
otherArray =取出(混乱); console.log(otherArray); – user2249160
'otherArray'不在函数的范围内。 –
'undefined'不是错误。你甚至没有任何错误。 – Xufox