我的第一个JavaScript类的作业是创建一个应用程序,它将姓氏,名字和分数作为学生的输入。应用程序应保持分数的运行平均值,并提供清除分数的按钮,以及按姓氏排序的按钮。除了平均功能外,我的一切都在工作。我写了它,但它没有实现。我可能会遗漏一些简单的东西。我非常感谢您提供的任何帮助。谢谢你的时间!
.js文件:
var $ = function (id) {
return document.getElementById(id);
}
//the dreaded $ variable
var grades = [];
//array
//update scores with scores, first, last, sort by last
var update_scores = function () {
var val = get_item_list(grades);
$("scores").value = val;
$("last_name").value = "";
$("first_name").value = "";
$("score").value = "";
$("last_name").focus();
}
//function to add last, first, score to studentGrade
var student_grade_add_click = function () {
var last = $("last_name").value;
var first = $('first_name').value;
var score = parseFloat($('score').value);
grades.push([last, first, score]);
update_scores();
}
var get_item_list = function (item_list) {
if (item_list.length == 0) {
return "";
}
var list = "";
for (var i = 0; i < item_list.length; i++) {
var current = item_list[i];
for (var attribute in current) {
list += current[attribute] + ", ";
}
list += "\n";
}
return list;
}
//average function
function getAverageScore(grades) { //function-takes grades array
var i; //declare variable
var numberOfStudents = grades.length; //declare variable along length of array for number of students
var sum = 0; //set sum to zero
if (numberStudents > 0) { // step through grades if > 0 students
for (i = 0; i < numberOfStudents; i++) { //increment by 1 through grades
sum += grades[i][2]; //add each score to sum
}
return sum / numberOfStudents; //divide total of sum by how many students
}
return 0; // if no students, return 0
}
function clear_click() {
$("form").reset();
grades.splice(0, grades.length); //TO CLEAR ARRAY AS WELL AS FORM
}
var sort_click = function () //ENTIRE FUNCTION TO SORT ARRAY AND REPRINT
{
grades.sort();
update_scores();
}
window.onload = function () {
$("add_button").onclick = student_grade_add_click;
$("sort_button").onclick = sort_click;
$("last_name").focus();
}
这是我的.html文件:
<!DOCTYPE html>
<head>
<title>Part 4</title>
<link rel="stylesheet" type="text/css" href="scores.css" />
<script type="text/javascript" src="scores.js"></script>
<link href='http://fonts.googleapis.com/css?family=Molengo' rel='stylesheet' type='text/css'>
</head>
<body>
<div id="content">
<h1>Student Scores</h1>
<div class="formLayout">
<label>Last Name:</label>
<input type="text" id="last_name"/><br/>
<label>First Name:</label>
<input type="text" id="first_name"/><br/>
<label>Score:</label>
<input type="text" id="score"/><br/>
<label> </label>
<input type="button" id="add_button" value="Add Student Score"/><br/>
</div>
<h2>Student Scores</h2>
<form id = "form">
<p><textarea id="scores" rows="5" cols="60" disabled="disabled"></textarea></p>
</form>
<div class="formLayout">
<label>Average score:</label>
<input type="text" id="average_score" disabled="disabled"/><br/>
<label> </label>
<input type="button" id="clear" value="Clear Student Scores" onclick="clear_click()"/><br/>
<label> </label>
<input type="button" id="sort_button" value="Sort By Last Name"/><br/>
</div>
</body>
</html>
答案 0 :(得分:1)
您尚未调用函数getAverageScore(grades)
。所以只需在update_scores函数中添加调用
var update_scores = function () {
var val = get_item_list(grades);
$("scores").value = val;
$("last_name").value = "";
$("first_name").value = "";
$("score").value = "";
$("last_name").focus();
$("average_score").value = getAverageScore(grades);
}
在getAverageScore(grades)
函数中,numberOfStudents
条件中的变量为numberStudents
而不是if
。
function getAverageScore(grades) { //function-takes grades array
var i; //declare variable
var numberOfStudents = grades.length; //declare variable along length of array for number of students
var sum = 0; //set sum to zero
if (numberOfStudents > 0) { // step through grades if > 0 students
for (i = 0; i < numberOfStudents; i++) { //increment by 1 through grades
sum += grades[i][2]; //add each score to sum
}
return sum / numberOfStudents; //divide total of sum by how many students
}
return 0; }
经过这些修改后,代码运行正常!