计算目标出勤率所需的最低讲座%

时间:2017-01-10 18:08:40

标签: javascript

这个概念很简单, 假设我参加了4个讲座中的3个。我目前的百分比是75% 我希望%95以上。这意味着我需要再参加16门课程,使其成为20个讲座中的19个。

这是我在JS中实现的。我需要在结尾处乘以10 - 不知道为什么答案是正确答案的十分之一。



var present=3, absent=1;
var total = present+absent;
var CurrentPercentage = (100*present)/total;

var classReq = (95 * total)/100 - present;
classReq += (95 * classReq)/100;

console.log(classReq>0?(Math.ceil(classReq*10)):0);




它有效,但我认为必须有更好的算法(我相信一定有)

2 个答案:

答案 0 :(得分:1)

基本上你有一个讲座数(20)和一个百分比值(95%)和参观讲座(3),你可以计算缺失的数量并减去已经访问过的讲座。



var target = 95,
    lectures = 20,
    visited = 3,
    needed = lectures * target / 100 - visited;

console.log(needed);




您可以查看错过的讲座,然后您可以计算所需的讲座。



var target = 95,  // percent
    visited = 3,
    missed = 1,   // this is 5%
    needed = missed * 100 / (100 - target) - visited;

console.log(needed);




答案 1 :(得分:0)

怎么样:

current=3;
max=4;
if(current/max>0.95){
 alert("reached");
}else{
  Alert((0.95-current/max)*100+"% needed");
}