Javascript-如果权重已移除,则按比例重新分配权重

Javascript-如果权重已移除,则按比例重新分配权重,javascript,arrays,javascript-objects,Javascript,Arrays,Javascript Objects,我有一组数据,列出了一次考试对最终成绩的贡献比例。如果一个学生错过了某个/某些测试,那么权重将在他参加的测试中按比例重新分配。我想计算学生参加这些考试的新比例。如何将数组与对象数组进行比较,以确定学生缺席的测试?至于修正比例的计算,我正在考虑使用if语句,有人能想出一种更有效的方法来实现这一点吗?谢谢大家! testPercentage = {"A":0.5,"B":0.3,"C":0.15,"D":0.05}; m

我有一组数据,列出了一次考试对最终成绩的贡献比例。如果一个学生错过了某个/某些测试,那么权重将在他参加的测试中按比例重新分配。我想计算学生参加这些考试的新比例。如何将数组与对象数组进行比较,以确定学生缺席的测试?至于修正比例的计算,我正在考虑使用
if
语句,有人能想出一种更有效的方法来实现这一点吗?谢谢大家!

testPercentage = {"A":0.5,"B":0.3,"C":0.15,"D":0.05};
maryTest = ["A", "B"];

revised test percentage for Mary would now be:
A = 0.5/(1-0.15-0.05)
B = 0.3/(1-0.15-0.05)


if (maryTest.length == 1){
//assume Mary has only written test A
    A = 1
}
if (maryTest.length == 2){
    A = 0.5 + (0.5/(1-the proportion of those two tests she has missed))
    B = 0.3 + (0.3/(1-the proportion of those two tests she has missed))
}
if (maryTest.length == 3){
//
}
if (maryTest.length == 4){
// same as testPercentage 
}

这更像是一个数学问题,而不是编程。没那么复杂

我认为你的公式是错误的。修订后的A应为
0.5/0.8
,而不是大于1的
0.5+0.5/0.8

VarTestPercentage={“A”:0.5,“B”:0.3,“C”:0.15,“D”:0.05}; var maryTest=[“A”,“B”]; var maryWeights=maryTest.map(key=>[key,testPercentage[key]])/[[A],0.5],“B”,0.3]] var totalWeight=maryWeights.map(条目=>条目[1]).reduce((a,b)=>a+b)//0.8 var revisedTestWeights=Object.fromEntries(maryWeights.map(([key,value])=>[key,value/totalWeight]))
testPercentage的值总和总是等于1吗?这是伪代码还是实际值?@samanime是的,你说得对,我刚刚编辑了这篇文章。谢谢谢谢你,我会测试出来的!是的,你是对的,我刚刚更新了帖子。