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

好久不见. 提交于 2021-01-29 06:00:31

问题


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


回答1:


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);


来源:https://stackoverflow.com/questions/57219697/choose-a-number-from-a-range-based-on-percentage-of-another-number

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!