如果我知道一个数组中相应位置的索引,如何识别并行数组中的相应位置?

时间:2011-06-28 11:52:33

标签: javascript arrays for-loop

我有一个简单的JavaScript程序来计算选举的胜利者。 我使用FOR循环将每个候选人投票的总数分配到一个名为totalVotesArray的新数组中,然后我将相应的候选人姓名和总投票输出到浏览器窗口中。

然后我需要输出得分最高的候选人 - 我已经使用了另一个FOR循环,但我不确定我是否正确地完成了那个部分。我找不到获胜分数的索引,并将其与并行数组中的相应位置相关联。

有人知道我是怎么做到的吗?

<HTML>
<HEAD>
<TITLE>
Election Results
</TITLE>
<LINK REL="stylesheet" TYPE="text/css" HREF="tma03.css">
<script type="text/javascript">

var candidateArray = ['Ms A  Brown .......', 
                      'Mr C Smith .......', 
                      'Ms F Patel .......', 
                      'Ms B Jones .......', 
                      'Mr E Williams...', 
                      'Mr D Johnson ....', 
                      'Ms G Taylor'];
var onlineVotesArray = [41,37,43,11,59,21,36];
var paperVotesArray = [22,3,15,11,7,1,18];


//initialises totalVotesArray with a new empty array the same size as candidateArray
var totalVotesArray = new Array(candidateArray.length)

//for loop counts the number of times the addition of the online and paper votes should be performed
for (var i = 0; i <= candidateArray.length; i = i + 1)
{
/*adds elements at the position in the onlineVotesArray Array to the element in the same position in the paperVotesArray Array, and stores result in corresponding position in the total votes array  */
    totalVotesArray[i] = onlineVotesArray[i] + paperVotesArray[i];
}
//outputs the election report heading and results to the browser document window
document.write('Eatanswill Historical Society By Election' + '<BR>' + 'Declaration of Results' + '<BR>' + '<BR>');
document.write(candidateArray[0] + totalVotesArray[0] + '<BR>');
document.write(candidateArray[1] + totalVotesArray[1] + '<BR>');
document.write(candidateArray[2] + totalVotesArray[2] + '<BR>');
document.write(candidateArray[3] + totalVotesArray[3] + '<BR>');
document.write(candidateArray[4] + totalVotesArray[4] + '<BR>');
document.write(candidateArray[5] + totalVotesArray[5] + '<BR>');
document.write(candidateArray[6] + totalVotesArray[6] + '<BR>');
//this outputs an extra line break
document.write('<BR>');


//debugger;
var maximumTotalVoteIndex = 0;

for (var count = 1; count < totalVotesArray.length; count = count + 1)
{
    if (totalVotesArray[count] > maximumTotalVoteIndex)
        {
            maximumTotalVoteIndex = totalVotesArray[count];
        }
}

document.write( **THIS IS THE BIT I'M STUCK WITH** + ' is declared the winner');
</SCRIPT>

</HEAD>
<BODY>
</BODY>
</HTML>

3 个答案:

答案 0 :(得分:1)

试试这个

var maximumTotalVoteIndex = 0;
    var maximumVote=totalVotesArray[0];

    for (var count = 1; count < totalVotesArray.length; count = count + 1)
    {
        if (totalVotesArray[count] > maximumVote)
            {
                maximumVote = totalVotesArray[count];
                maximumTotalVoteIndex = count;
            }
    }

    document.write("Highest scoring Candidate is "+candidateArray[maximumTotalVoteIndex]+"with votes"+maximumVote);

答案 1 :(得分:0)

  

maximumTotalVoteIndex = totalVotesArray [count];   有一个主要问题。

totalVotesArray [count]是第count个人的投票数,而maximumTotalVoteIndex是个人。

所以你应该保持maximumTotalvoteIndex = count。

顺便说一句,count应该从for循环中的0开始。

答案 2 :(得分:0)

此循环上的拼写错误应为<而不是<=所以

//for loop counts the number of times the addition of the online and paper votes should be performed
for (var i = 0; i < candidateArray.length; i++)

然后以最简单的方式,因为你必须考虑关系;

//get max value;
var maximumTotalVoteValue = 0;
for (var count = 0; count < totalVotesArray.length; count++) {
    if (totalVotesArray[count] > maximumTotalVoteValue) {
        maximumTotalVoteValue = totalVotesArray[count];
    }
}

//find ppl with maximum vote
var winners= [];
for (var count = 0; count < totalVotesArray.length; count++) {
    if (totalVotesArray[count] === maximumTotalVoteValue) {
        winners.push(candidateArray[count]);
    }
}

//show results
if (winners.length === 1) {
    document.write(winners[0] + " is declared the winner");
} else {
    document.write(" Tied: " + winners.join(" and "));
}

document.write(", with: " + maximumTotalVoteValue );
相关问题