Muhammad Humza
Muhammad Humza

Reputation: 21

choose a number from a range based on percentage of another number

I want to select a number between two numbers depends on the percentage of an input. lets say the range is between 40-60. and input range is between 1-10. if the input value is 10 , the output should be 60. value = 1 , output = 40. value = 5 , output = 50.

I am first trying to figure the algorithm, how to begin with

so far I have used various different formulas. In general, to scale your variable x into a range [a,b] you can use:

normalized = ((b−a)x−min(x))(max(x)−min(x))+a

https://stats.stackexchange.com/a/281165

Upvotes: 0

Views: 506

Answers (1)

spender
spender

Reputation: 120480

So, a higher-order function might be useful here:

const func = (outMin, outMax, inMin, inMax) => 
             v => outMin + (outMax - outMin) * (v - inMin) / (inMax - inMin);

const boundFunc = func(40, 60, 1, 10);

const v1 = boundFunc(1); //40
const v2 = boundFunc(5); //48.8888....
const v3 = boundFunc(10); //60

console.log(v1, v2, v3);

Upvotes: 2

Related Questions