map

Convert a value in one number range to a value in another number range.

Math.map(0, 0, 0, 0, 0)

A map is a conversion of one span of numbers to another. If a dog can live to 16 years and a person lives to 87 years, how do you make a 16 year life span seem like a 87 year life span? You say that one dog year is like some number of people years. This is a mapping of dog years to people years.

Fahrenheit and Celsius are different ways to measure temperature. Celsius doesn’t use the same amount of degrees as Fahrenheit. So, there is more energy in one degree of Celsius. If you want to convert a temperature value of Fahrenheit (something between freezing and boiling maybe) to Celsius, you can use Math.map(50, 32, 212, 0, 100). The map makes 50 degrees of Fahrenheit turn into 10 degrees of Celsius.

Parameters

  • value: a number to convert from one range to another.
  • fromLow: the low number of the range to convert from.
  • fromHigh: the high number of the range to convert from
  • toLow: the low number of the range to convert to.
  • toHigh: the high number of the range to convert to.

Returns

  • a new value that is a number converted from original value using a mapping of the from range into the to range.

Example

Use a lifespan map to find out the age in “people years” of a dog that’s 7 years old.

let dogsAge = 7 
let peoplesAge = Math.map(dogsAge, 1, 16, 15, 87) 

See also

constrain