我正在尝试解决变量x
。我想我的D
变量有问题。该D
变量应该将每个输入w
的 . 例如,D = w[0]+w[1]+w[2]
如果有 3 个输入或D = w[0]+w[1]
有 2 个输入。对于实际值,goal_grade
=90.0、=20、4exam_weight
个额外类别、w[1]
=10、p[1]
=100、w[2]
=20、p[2]
=95.98、w[3]
=30、p[3]
=95.47 和w[4]
=20、p[4]
=96.47...x
应该等于大约 62,但我得到一些巨大的数字。
<html>
<head>
<script type='text/javascript'>
var w = [];
var p = [];
var C = 0;
var D = 0;
function addFields() {
// Number of inputs to create
var number = document.getElementById("category_weight").value;
// Container <div> where dynamic content will be placed
var container = document.getElementById("container");
// Clear previous contents of the container
while (container.hasChildNodes()) {
container.removeChild(container.lastChild);
}
for (i = 0; i < number; i++) {
// create a row element to contain each pair
var row = document.createElement("div");
row.id = 'row' + i
row.appendChild(document.createTextNode("Category " + (i + 1) + " weight: "));
var weight_input = document.createElement("input");
weight_input.type = "number";
weight_input.name = "weight";
row.appendChild(weight_input);
row.appendChild(document.createTextNode("Category " + (i + 1) + " percentage: "));
var percentage_input = document.createElement("input");
percentage_input.type = "number";
percentage_input.name = "percentage";
row.appendChild(percentage_input);
// append inputs to row instead of container, then append each row to container
container.appendChild(row);
}
}
function weighted() {
var container = document.getElementById("container");
var rows = container.children;
for (var i = 0; i < rows.length; i++) {
var row = rows[i];
w[i] = row.children[0].value; // or row.querySelectorAll('[name=weight]').value;
p[i] = row.children[1].value;
}
for (var i = 0; i < rows.length; i++) {
// You can do as much calculation here with w[i] & p[i]
C += w[i] * p[i];
D += w[i];
}
console.log(C);
console.log(D);
var x = "";
var A = parseFloat(document.getElementById("goal_grade").value);
var B = parseFloat(document.getElementById("exam_weight").value);
x = (A * (D + B) - C) / B;
if (x <= 0) {
x = 0;
}
document.getElementById("result").innerHTML = x;
}
</script>
</head>
<body>
<span>What final percentage in the class are you trying to reach or stay above?</span>
<input type="number" id="goal_grade" name="goal_grade" />
<br>
<br>
<span>What percent is the final exam weighted?</span>
<input type="number" id="exam_weight" name="exam_weight" />
<br>
<br>
<span>How many extra weighted categories are there?</span>
<input type="number" id="category_weight" name="category_weight" value=""> <br />
<button type="button" onclick="addFields()">Submit </button>
<div id="container"></div>
<br>
<br>
<input type="button" value="Calculate" onclick="weighted()" />
<br>
<br>
<span>You will need to get <p id="result" name="r1"> </p> points out of the ... <p id="totpoints" name="tot_points"> </p> points possible on the final"
</span>
</body>
</html>