Files
30-seconds-of-code/snippets/js/s/map-num-range.md
2023-05-10 22:35:09 +03:00

22 lines
441 B
Markdown

---
title: Map number to range
type: snippet
language: javascript
tags: [math]
cover: carrots
dateModified: 2020-10-21T21:54:53+03:00
---
Maps a number from one range to another range.
- Return `num` mapped between `outMin`-`outMax` from `inMin`-`inMax`.
```js
const mapNumRange = (num, inMin, inMax, outMin, outMax) =>
((num - inMin) * (outMax - outMin)) / (inMax - inMin) + outMin;
```
```js
mapNumRange(5, 0, 10, 0, 100); // 50
```