我想解決變量x
。我想我的D
變量有問題。 D
變量應該將每個輸入的w
加起來。例如,如果有三個輸入,則爲D = w[0]+w[1]+w[2]
;如果有兩個輸入,則爲D = w[0]+w[1]
。具有實數值,與goal_grade
= 90.0,exam_weight
= 20,4個額外的類別,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>
提示:爲了改善問題,請嘗試將問題隔離爲只有JavaScript函數無法正常工作(例如,用示例javascript值替換輸入字段)。 –
其他提示:嘗試給出更詳細的問題描述以及你試圖解決的問題 –
這是否更好? –