Eilidh
Eilidh

Reputation: 1374

Math.floor(Math.random*myArray.length) is returning NaN

I'm receiving a bit of a strange error.

I'm trying to select the index of an array at random, so to do this I am using the code below:

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

myArray.length is 282, but Math.floor(Math.random*myArray.length) is NaN and myArray[Math.floor(Math.random*myArray.length) is undefined.

Any thoughts?

Upvotes: 2

Views: 1753

Answers (2)

SK.
SK.

Reputation: 4358

Math.random() and Math.random(), go through API links.

Your code will be:

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

Upvotes: -1

Arun P Johny
Arun P Johny

Reputation: 388316

Math.random is a function it should be Math.random()

Try

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

Upvotes: 9

Related Questions