hacker-scripts
hacker-scripts copied to clipboard
potential out of range error
This line: https://github.com/NARKOZ/hacker-scripts/blob/master/nodejs/hangover.js#L33
var excuse = excuses[Math.floor(Math.random() * excuses.length)];
should be:
~~var excuse = excuses[Math.floor(Math.random() * excuses.length - 1)];
~~
var excuse = excuses[Math.floor(Math.random() * (excuses.length - 1))];
@at0g you are wrong because Math.random() * excuses.length - 1
could give you a negative integer, the proper way is var excuse = excuses[Math.floor(Math.random() * excuses.length)%excuses.length];
The current code is totally fine, your code is wrong. Math.random returns a number between 0 (inclusive) and 1 (exclusive). So Math.random() * length can never be equal length, is always lower than length.
That's true @emre1702 , the line of code var excuse = excuses[Math.floor(Math.random() * excuses.length)];
works perfectly fine. No change must be performed. Making my change will cause an error in case that excuses = []