Last active
July 31, 2019 23:17
-
-
Save shiv19/43bc84ce18ca83f60947ce067911ccf5 to your computer and use it in GitHub Desktop.
convert a number from old range to new range
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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