Skip to content

Instantly share code, notes, and snippets.

@shiv19
Last active July 31, 2019 23:17
Show Gist options
  • Save shiv19/43bc84ce18ca83f60947ce067911ccf5 to your computer and use it in GitHub Desktop.
Save shiv19/43bc84ce18ca83f60947ce067911ccf5 to your computer and use it in GitHub Desktop.
convert a number from old range to new range
export function RangeConvertFactory({
oldMin, oldMax, newMin, newMax
}) {
const oldRange = oldMax - oldMin;
const newRange = newMax - newMin;
return (val) => (((val - oldMin) * newRange) / oldRange) + newMin;
}
// usage
const convert0to10TO5to10 = RangeConvertFactory({ oldMin: 0, oldMax: 10, newMin: 5, newMax: 10 });
for (let i=0; i<=10; i++) console.log(convert0to10TO5to10(i));
// 5
// 5.5
// 6
// 6.5
// 7
// 7.5
// 8
// 8.5
// 9
// 9.5
// 10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment