30秒学会 JavaScript 片段 · 2023年3月1日

30秒学会 JavaScript 片段 – Map number to range

Maps a number from one range to another range.

  • Return num mapped between outMinoutMax from inMininMax.

代码实现

const mapNumRange = (num, inMin, inMax, outMin, outMax) =>
  ((num - inMin) * (outMax - outMin)) / (inMax - inMin) + outMin;

mapNumRange(5, 0, 10, 0, 100); // 50

翻译自:https://www.30secondsofcode.org/js/s/map-number-to-range