配列を作成する必要がある宿題があります。私のコードは機能しますが、配列を使用していると見なされるかどうかはわかりません。誰かが私のためにこれを明確にして、配列に置き換えることができるコードを教えてくれたら、とても感謝しています!
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Chapter 09 Arrays</title>
</head>
<body>
<h1>Student Scores</h1>
<form>
<table>
<tr style="background-color:grey">
<td width='25%' align='center'><b>First Name</B></td>
<td width='25%' align='center'><b>Last Name</b></td>
<td width='25%' align='center'><b>Score<b></td>
<td width='25%' align='center'> </td>
</tr>
<tr>
<td><input type="text" name="first_name" id='first_name'></td>
<td><input type="text" name="last_name" id='last_name'></td>
<td><input type="text" name="score" id='score' style='text-align:right'></td>
<td width='25%' align='center'>
<input type="button" value="Add Student Score" onClick='javascript:addScore();'>
</td>
</tr>
</table>
</form>
<h2>Student Scores</h2>
<textarea rows="4" cols="50" readonly name='results' id='results'>
</textarea>
<br />
Average Score: <input type="text" name="avg" id='avg' disabled="disabled"><br>
<br />
<input type="reset" value="Reset">
<input type="button" value="Sort By Last Name" onClick='javascript:sortScore();'>
<input type="button" value="Clear Scores" onClick='javascript:clearScores();'>
<script type="text/javascript">
var firstName = [];
var lastName = [];
var score = [];
var aDisplay = [];
function resetField() {
}
function clearScores() {
firstName = [];
lastName = [];
score = [];
aDisplay = [];
document.getElementById('avg').value='';
document.getElementById('results').value='';
resetEntry();
}
function resetEntry() {
document.getElementById('first_name').value='';
document.getElementById('last_name').value='';
document.getElementById('score').value='';
}
function addScore() {
var tempFirstName = getFieldValue('first_name');
var tempLastName = getFieldValue('last_name');
var tempScore = getFieldValue('score');
if (tempFirstName == '') {
alert('Specify a value for first name');
document.getElementById('first_name').focus();
return;
}
if (tempLastName == '') {
alert('Specify a value for last name');
document.getElementById('last_name').focus();
return;
}
if (tempScore == '') {
alert('Specify a value for score');
document.getElementById('score').focus();
return;
}
temp = "";
total = 0;
var tempAvg = 0.0;
firstName.push(getFieldValue('first_name'))
lastName.push(getFieldValue('last_name'))
score.push(getFieldValue('score'))
aDisplay.push(getFieldValue('last_name') + "," + getFieldValue('first_name') + "," + parseInt(getFieldValue('score')))
for(i = 0; i < score.length; i++) {
total += parseInt(score[i]);
}
tempAvg = (total / score.length);
tempAvg = tempAvg.toFixed(2);
document.getElementById('avg').value = tempAvg;
listScores();
resetEntry();
}
function sortScore() {
aDisplay.sort();
listScores();
}
function listScores() {
temp = "";
document.getElementById('results').value = '';
for(i = 0; i < aDisplay.length; i++) {
temp += aDisplay[i] + "\n";
}
document.getElementById('results').value = temp;
}
function getFieldValue(p) {
var v = "";
if (document.getElementById(p) != null && document.getElementById(p).value > "") {
v = document.getElementById(p).value;
}
return v;
}
clearScores();
</script>
</body>
</html>