Stu*_*art 2 javascript math rating-system
我想知道是否有更好的方法来创建stars基于百分比值的变量.
我只需要根据百分比值确定1,1,5,2,2.5等等级的等级.
我用switch语句完成了它,但是想知道是否有一个数学公式可以做到这一点?
看起来似乎有点长篇大论,以这种方式做到这一点?
var stars = 0;
switch (true) {
case score === 100:
stars = 5;
break;
case score >= 90:
stars = 4.5;
break;
case score >= 80:
stars = 4;
break;
case score >= 70:
stars = 3.5;
break;
case score >= 60:
stars = 3;
break;
case score >= 50:
stars = 2.5;
break;
case score >= 40:
stars = 2;
break;
case score >= 30:
stars = 1.5;
break;
case score >= 20:
stars = 1;
break;
case score >= 10:
stars = 0.5;
break;
case score >= 0:
stars = 0;
break;
}
Run Code Online (Sandbox Code Playgroud)
我承认我的数学技能不是很好(我对数字有阅读困难)很抱歉,如果我的问题冒犯了那些严格要求自己尝试的东西,但这只是在煎我的大脑!
在此先感谢您提供的任何帮助!
| 归档时间: |
|
| 查看次数: |
219 次 |
| 最近记录: |