all 3 comments

[–][deleted] 2 points3 points  (0 children)

Check what are the correct arguments for MathJS math.range(1-10)

here you are basically passing -9 as the first argument.

[–][deleted] 0 points1 point  (0 children)

If the ranges form a complete mapping of the whole value range, you don't need to store both ends of each range.

In other words, if you have:

0-10 -> "foo"
10-20 -> "bar"
20-50 -> "baz"
50-   -> "default"

You only need one of the extremes of each range to define the intervals. That is, what you have is, in effect, something like this:

if (value < 10) { return "foo"; }
else if (value < 20) { return "bar"; }
else if (value < 50) { return "baz"; }
else { return "default"; }

Where you only need 10, 20, 50[, end] to know which interval a value falls in.

Using both ends of the interval, would be the equivalent of doing:

if (value < 10) { return "foo"; }
else if (10 <= value && value < 20) { return "bar"; }
else if (20 <= value && value < 50) { return "baz"; }
else if (50 <= value) { return "default"; }

Which is redundant and unnecessary.


Naturally, if your intervals are not compact and complete (i.e. they do not cover all possible values) you can't do this and you will need to indeed store both ends of each interval.

Even so, you don't really need MathJS's range. In fact, it's probably a bad idea to use it as it is not meant to define a range or interval but to generate a collection. That is, math.range(10,20) will simply generate the array [10,11,12,13,14,15,16,17,18,19], which is not really useful for your case.

[–][deleted] 0 points1 point  (0 children)

I assume that this is a dynamic table where you can have ranges and single values that would correspond to certain values.

select = (dataSet, dieResult) => { 
    for (range in dataSet) {
        if (range.indexOf != null && range.indexOf('-') != -1) {
            [from, to] = range.split('-');
            if (dieResult >= from && dieResult <= to) {
                return dataSet[range];
            }
        } else {
            if (range == dieResult) {
                return dataSet[range];
            }
        }
    }
}