Math.floor(Math.random*myArray.length)正在返回NaN

Eil*_*idh 2 javascript

我收到一点奇怪的错误.

我试图随机选择一个数组的索引,所以为了做到这一点,我使用下面的代码:

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

myArray.length是282,但是Math.floor(Math.random*myArray.length)NaN并且myArray[Math.floor(Math.random*myArray.length)是未定义的.

有什么想法吗?

Aru*_*hny 9

Math.random 它应该是一个功能 Math.random()

尝试

myArray[Math.floor(Math.random()*myArray.length)]
Run Code Online (Sandbox Code Playgroud)