0

我有一个家庭作业,要求我制作一个数组。我的代码有效,但我不确定它是否被视为使用数组。如果有人能为我澄清这一点并告诉我可以用数组替换哪些代码,我将不胜感激!

<!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'>&nbsp;</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>
4

2 回答 2

2

在这里,我会尽力帮助你:

  1. 通用术语http://en.wikipedia.org/wiki/Array#In_computer_science
  2. 如果您在 JavaScript 编程语言的上下文中了解数组,那么(大多数情况下)您最好在 Mozilla 开发人员网络 (MDN) https://developer.mozilla.org/en-US/docs/JavaScript/上阅读它参考/Global_Objects/数组

由于这是你的家庭作业,试着理解这个术语是什么以及它在你的作业上下文中的含义。

于 2012-12-02T21:45:08.307 回答
0

所有这些都非常清楚你使用数组,并且正确。

var firstName = [];
var lastName = [];
var score = [];
var aDisplay = [];
.......
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]);
}
于 2012-12-02T21:45:33.347 回答