diff --git a/mapNumRange.md b/mapNumRange.md new file mode 100644 index 000000000..df12eb8ef --- /dev/null +++ b/mapNumRange.md @@ -0,0 +1,13 @@ +### mapNumRange + +Maps a number from one range to another range. + +Returns `num` mapped between `outMin`-`outMax` from `inMin`-`inMax`. + +```js +const distance = (num, inMin, inMax, outMin, outMax) => (num - inMin) * (outMax - outMin) / (inMax - inMin) + outMin; +``` + +```js +mapNumRange(5,0,10,0,100); // 50 +``` diff --git a/mapNumRange.test.js b/mapNumRange.test.js new file mode 100644 index 000000000..77ca11e8d --- /dev/null +++ b/mapNumRange.test.js @@ -0,0 +1,9 @@ +const expect = require('expect'); +const {mapNumRange} = require('./_30s.js'); + +test('mapNumRange is a Function', () => { + expect(mapNumRange).toBeInstanceOf(Function); +}); +test('Maps 5 to the range 0-100 from 0-10', () => { + expect(distance(5,0,10,0,100)).toEqual(50); +}); diff --git a/tag_database b/tag_database index 0636ee018..785aa026e 100644 --- a/tag_database +++ b/tag_database @@ -179,6 +179,7 @@ longestItem:array,string,utility,intermediate lowercaseKeys:object,intermediate luhnCheck:math,utility,advanced mapKeys:object,function,intermediate +mapNumRange:math,beginner mapObject:array,object,advanced mapString:string,array,function,utility,beginner mapValues:object,function,intermediate