【发布时间】:2015-07-28 17:36:03
【问题描述】:
如何在输入时将提示输入的数据添加到数组中,然后除以数组大小为 4 的数量?我真正需要帮助的唯一一件事是将输入的数据相加并将其除以显示平均值。
<html>
<body>
<script type="text/javascript">
// declare variables and constants
var SIZE = 4 // array size
var gradeArray // array variable
var gradeAvg // grade average variable
var avgCalc = parseFloat(avgCalc); // variable used to store avg grade
var letGrad = "" // variable used to store Letter Grade
var userInput // variable used for while function
var index // loop variable
var BR = "<br></br>" // page break
var ES = "" // empty string
var semi = ":"
// Create Array
gradeArray = new Array(SIZE);
}
// Create Loop structure for user grade input and ask if input is correct
while (userInput != "Y") {
for (index = 0; index < SIZE; index++) {
gradeArray[index] = prompt("Enter your grades # " + (index + 1) + ":" + ES);
}
// Create display function for loop structure, Displays user input of grades
document.write("Grades Entered:" + BR);
for (index = 0; index < SIZE; index++) {
document.write("The grades you entered are: ");
document.write(gradeArray[index] + BR);
}
userInput = prompt("Are the these grades correct? (Y/N)");
if (userInput == "Y") {
document.write("Thank you!" + BR);
}
}
// Create selection structure. This structure gives the average a letter grade
if (avgCalc >= 90) {
letGrad = "A";
}
else if (avgCalc >= 80) {
letGrad = "B";
}
else if (avgCalc >= 70) {
letGrad = "C";
}
else if (avgCalc >= 60) {
letgrad = "D";
}
else if (avgCalc < 60) {
letGrad = "F";
}
// Display results -- display's results
document.write("Your average is: " + avgCalc + semi + letGrad + BR);
document.write("Thank you for using Average Calculator!");
</script>
</body>
</html>
【问题讨论】:
-
你能把所有的代码都包含进去吗,现在你少了一个大括号,我们不知道gradeArray是在哪里创建的。
-
如果您所做的只是计算平均值,则无需将值存储在数组中。
-
我不只是计算平均值。在那种情况下,id 只需将输入放入单独的变量中,然后使用函数?
标签: javascript arrays add