[英]Finding percentage values given min and max in JavaScript
I am very new to JavaScript and programming, so i hope you'll bear with me.我对 JavaScript 和编程很陌生,所以我希望你能忍受我。 I am trying to write a program that places a pointer on a scale.
我正在尝试编写一个将指针放在刻度上的程序。 The min and max values of the scale, as well as the values in between, changes depending on use.
刻度的最小值和最大值,以及它们之间的值,会根据使用情况而变化。 I am trying to define my minimum value as 0%, my max value as 100% and want to be able to place all other values in between.
我试图将我的最小值定义为 0%,将我的最大值定义为 100%,并希望能够将所有其他值放在两者之间。
sortedResults = testResults.sort((a, b) => a - b);
SortedMin = sortedResults[0];
SortedMax = sortedResults[sortedResults.length - 1];
console.log(SortedMin);
console.log(SortedMax);
the values are taken from the sortedResults array and I am able to find the min and max, but simply cant figure out where to start.这些值取自 sortedResults 数组,我能够找到最小值和最大值,但根本不知道从哪里开始。
You could take a closure over min
and max
value and subtract min
from value
and max
and get the percent value of both numbers.您可以对
min
和max
值进行闭包,然后从value
和max
中减去min
并获得两个数字的百分比值。
const getPercent = (min, max) => value => 100 * (value - min) / (max - min), testResults = [5, 2, 4, 3, 1], minVal = Math.min(...testResults), maxVal = Math.max(...testResults), sample = getPercent(minVal, maxVal); console.log(testResults.map(sample));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.