我正在尝试计算以下公式,以根据用户的输入返回0 - 100(等级)的数值,
成绩=考试成绩×考试成绩+(100% - 考试成绩)×当前成绩
并使用以下代码执行此操作,
var total = ExamWorth * ExamScore + (100 - ExamWorth) * CurGrade;
我的3个变量刚刚从html中拉入,
<input type="text" class="form-control" id="aimGrade">
然而,当我尝试计算时,我总是得到错误的答案,我认为不得不确定如何将其转换为小数。
例如,ExamWorth = 50 ExamScore = 80和curGrade = 70的输入应返回值90.我在同一输入时获得7500的返回值。
答案 0 :(得分:1)
我猜你正在混淆100%和100。
如果#content
是ExamWorth
和0
之间的值,则应使用1
代替1 - ExamWorth
。
100 - ExamWorth
如果var total = ExamWorth * ExamScore + (1 - ExamWorth) * CurGrade;
是ExamWorth
和0
之间的值,那么您需要除以100
。
100
使用您提供的示例,您将获得var total = (ExamWorth * ExamScore + (100 - ExamWorth) * CurGrade) / 100;
(而不是75
),这是计算最终成绩的正确数字。
答案 1 :(得分:1)
也许你可以输入你的输入数字:
<input type="number" class="form-control" id="aimGrade" min="0" max="100" />
正如您所看到的,它还允许您执行诸如添加最小值和最大值之类的奇特内容。
然后你仍然需要使用javascript parseFloat()函数:
var fExamWorth = parseFloat(ExamWorth);
最后,您可能需要重新审视计算。如果您的ExamWorth是100(即一个百分比)的值,您需要将最终结果除以100:
var total = (ExamWorth * ExamScore + (100 - ExamWorth) * CurGrade) / 100;
如果它实际上是十进制/比例(即介于0和1之间的值),那么它应该是:
var total = ExamWorth * ExamScore + (1 - ExamWorth) * CurGrade;
答案 2 :(得分:0)
您的问题不能转换为小数(当您除以100时,7500不会变为90)。你的算法错了。
请参阅评论中的算法说明。
另外(FYI),处理来自用户提供的数据的值时要记住两件事:
所以,这是一个例子(点击展开代码段):
var worth = document.getElementById("worth");
var score = document.getElementById("score");
var grade = document.getElementById("grade");
document.getElementById("calc").addEventListener("click", function(){
// There are a number (no pun intended) of ways to convert strings to numbers
var worthVal = parseInt(worth.value, 10); // explicit
var scoreVal = +score.value; // coercion
var gradeVal = Number(grade.value) // function
// Algorithm:
var examPer = (scoreVal / 100) * worthVal; // % of exam worth achieved
var gradeAvg = gradeVal + examPer / 2; // average of grades
// Order of operations: Parenthesis, Exponents, Multiplication, Division, Addition, Subtraction
// Because of the string, any operand on either side of it will normally be converted to a string
// If we were to try to do all the math at once, we have to be very careful about order of operations
console.log("Result 1: " + (gradeVal + ((scoreVal / 100) * worthVal) / 2));
// But, if we do the math in steps and just put the result with the message, it's much simpler
console.log("Result 2: " + gradeAvg);
});
&#13;
<input type="text" class="form-control" id="worth" placeholder="exam worth" value="50">
<input type="text" class="form-control" id="score" placeholder="score" value="80">
<input type="text" class="form-control" id="grade" placeholder="grade" value="70">
<input type="button" value="Calculate" id="calc">
&#13;
<强>更新强>
经过一番思考后,我认为计算应该按照您的预期返回90
。我认为它应该返回75
。这就是原因:
您正在向我们提供Exam Worth
并在计算中使用它,但您需要的唯一原因是获得分数(以分为单位),例如:
Exam is weighted at 50
You got 80 percent correct
.8 x 50 = 40 (You got a score of 40 and a percent of 80)
但是,为了获得整体成绩,我们并不关心考试成绩为40分的事实。我们只关心您获得80%的事实,因此Exam Weight
无关紧要。
要获得正确的整体成绩,我们需要做的就是添加两个等级(70和80)并除以2(75)。
因此,在这种情况下,代码将是:
var worth = document.getElementById("worth");
var score = document.getElementById("score");
var grade = document.getElementById("grade");
document.getElementById("calc").addEventListener("click", function(){
var scoreVal = +score.value;
var gradeVal = Number(grade.value)
// Algorithm:
var gradeAvg = (scoreVal + gradeVal) / 2; // average of grades
// But, if we do the math in steps and just put the result with the message, it's much simpler
console.log("Result 2: " + gradeAvg);
});
&#13;
<input type="text" class="form-control" id="worth" placeholder="exam worth" value="50">
<input type="text" class="form-control" id="score" placeholder="score" value="80">
<input type="text" class="form-control" id="grade" placeholder="grade" value="70">
<input type="button" value="Calculate" id="calc">
&#13;