score = 50;
thresholdMax = 70;
thresholdMin = 30;
if (score > thresholdMax) return 1;
if (score < thresholdMin) return 0;
else return //the fraction between thresholdMax and thresholdMin that score is, as a value between 0 and 1.
我知道这一定很容易,但我似乎无法想到如何写它。
我试过这样,但我觉得它没有用:
spread = ThresholdMax - ThresholdMin;
diff = score - ThresholdMin;
return Math.round(diff / spread);
答案 0 :(得分:3)
您还可以尝试使用min
和max
功能:
return min(1, max(0, (score-thresholdMin)/(thresholdMax-thresholdMin)));
答案 1 :(得分:2)
类似的问题:
spread = maxThreshold - minThreshold;
diff = score - minThreshold;
return Math.round(diff / spread);
当score
介于两个阈值之间时,diff / spread
将是一个介于0和1之间的数字,这意味着舍入它将永远只会给你零或一,而不是你想要的分数值。
如果 中的最小值和最大值(如问题所示),请使用:
fraction = (score - thresholdMin) / (thresholdMax- thresholdMin);
样本分数为:
score fraction
----- --------
31 0.025
35 0.125
40 0.250
45 0.375
50 0.500
69 0.975
答案 2 :(得分:1)
最简单的陈述应该是:
if (score > thresholdMax) return 1;
if (score < thresholdMin) return 0;
else{
return (score-thresholdMin)/(thresholdMax-thresholdMin);
}