我在哪里放一个JavaScript数组?

时间:2012-12-02 21:41:59

标签: javascript arrays

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

<!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>

2 个答案:

答案 0 :(得分:2)

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

  1. 一般术语http://en.wikipedia.org/wiki/Array#In_computer_science
  2. 如果你在JavaScript编程语言的上下文中学习数组,那么(在大多数情况下)你最好在Mozilla Developer Network(MDN)上阅读它https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array
  3. 因为这是你的作业,试着理解这个术语是什么以及它在你的作业背景下意味着什么。

答案 1 :(得分: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]);
}