Prototype functions

How to map one set of numbers to another set of values: = function (in_min, in_max, out_min, out_max) {
return (this - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;


How to constrain a number between a minimum value and a maximum value:

Number.prototype.constrain = function (min, max) {
return (Math.min(max, Math.max(min, this)));


These functions can be used together like so:

# Here we show a number with a value of 123
# the Number map function will map the values between 0 and 500
# to 0 and 100, but it will not take account for if the input number is
# higher or lower, to fix this we can now pipe it to the constrain function and constrain it there.

(123).map(0, 500, 0, 100).constrain(0, 100);

Result: 24.6


Posted on: Thursday 14 December 2017