Skip to content
Advertisement

Rolling dice project: code gives out of bounds exception below 7 and fails to save the first “Die Number”

I understand what out of bounds exception means, and how it happens, but I can’t find out why it’s happening in my code. Also, the output “Count for side 1” always states 0. This is my first post, but I think I am posting this right.

This is where I think the problem is.

JavaScript

Advertisement

Answer

Method RNG(randomNum, min, max) is expected to return values in the range [min...max] (inclusive), while dieNumber as the index in count array needs to be in the range [0; numberOfSides), and the following relation exists numberOfSides == max - min + 1.

So, a correction is needed to transform dieNumber into a valid index:

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement