I remember using an equation to do this at some point – how do you do this in Javascript?
Plugin two number ranges:
rangeX = 1 (through) 10;
rangeY =
I turned @Foggzie's answer into a TypeScript function and ES2016 function.
TypeScript:
const scale = (inputY: number, yRange: Array, xRange: Array): number => {
const [xMin, xMax] = xRange;
const [yMin, yMax] = yRange;
const percent = (inputY - yMin) / (yMax - yMin);
const outputX = percent * (xMax - xMin) + xMin;
return outputX;
};
ES2016:
const scale = (inputY, yRange, xRange) => {
const [xMin, xMax] = xRange;
const [yMin, yMax] = yRange;
const percent = (inputY - yMin) / (yMax - yMin);
const outputX = percent * (xMax - xMin) + xMin;
return outputX;
};