Javascript Array Random Got Zero -
i'm trying random number between 0 , array.length. have this:
getrandom() { const cars = object.keys(this.index); const randomint = math.floor(math.random() * cars.length); return cars[randomint]; } i ran few times , found 0 in 1 of results getrandom(). there not key in this.index object named 0.
is math function wrong?
update
after reading comments, , know getrandom() not wrong. have reset() function if guys can @ it.
reset() { const cars = object.keys(this.index); let = cars.length; while (i--) { this.index[cars[i]] = 0; } } is possible i'm adding new key 0 @ this.index object?
i can't see actual problem here.
object.keys turn named keys numbers (look here https://developer.mozilla.org/de/docs/web/javascript/reference/global_objects/object/keys), numbers starts 0.
so function, wrote yourselv, return an:
random number between 0 , array.length
Comments
Post a Comment