I'm trying to get random number between 0 and Array.length. I have this:
getRandom() {
const cars = Object.keys(this.index);
const randomInt = Math.floor(Math.random() * cars.length);
return cars[randomInt];
}
I ran this few times and found a 0
in one of the results from getRandom()
. There is not key
in my this.index
object named 0
.
Is my math function wrong?
UPDATE
After reading the comments, and I know the getRandom()
is not wrong. I also have reset()
function if you guys can look at it.
reset() {
const cars = Object.keys(this.index);
let i = cars.length;
while (i--) {
this.index[cars[i]] = 0;
}
}
Is it possible I'm adding a new key 0
at this.index
object?