2013-05-16 43 views
2

我收到了一些奇怪的錯誤。Math.floor(Math.random * myArray.length)正在返回NaN

我試圖隨機選擇一個數組的索引,所以要做到這一點,我用下面的代碼:

myArray[Math.floor(Math.random*myArray.length)]

myArray.length是282,但Math.floor(Math.random*myArray.length)是NaN和myArray[Math.floor(Math.random*myArray.length)未定義。

有什麼想法?

回答

9

Math.random是一個函數應該是Math.random()

嘗試

myArray[Math.floor(Math.random()*myArray.length)]