Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如果我知道一个数组中对应位置的索引,如何识别并行数组中的对应位置?_Javascript_Arrays_For Loop - Fatal编程技术网

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

Javascript 如果我知道一个数组中对应位置的索引,如何识别并行数组中的对应位置?,javascript,arrays,for-loop,Javascript,Arrays,For Loop,我有一个简单的JavaScript程序来计算选举的获胜者。 我使用FOR循环将每个候选人的总投票数分配到一个名为totalVotesArray的新数组中,然后将相应的候选人名称和总投票数输出到浏览器窗口中 然后,我需要输出得分最高的候选对象-我使用了另一个FOR循环,但我不确定我是否正确地完成了该部分。我一直在寻找中奖分数的索引,并将其与平行数组中的相应位置联系起来 有人知道我是怎么做到的吗 <HTML> <HEAD> <TITLE> Election Res

我有一个简单的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>

选举结果
var candidateArray=['Ms A Brown……',
“C史密斯先生……”,
“帕特尔女士……”,
“琼斯女士……”,
“威廉斯先生……”,
“约翰逊先生……”,
“G Taylor女士”];
var onlineVotesArray=[41,37,43,11,59,21,36];
var paperVotesArray=[22,3,15,11,7,1,18];
//使用与candidateArray大小相同的新空数组初始化TotalVoteArray
var totalVoteArray=新数组(candidateArray.length)
//对于循环计数,应执行在线和纸面投票相加的次数
对于(var i=0;i最大TotalVoteIndex)
{
maximumTotalVoteIndex=totalVotesArray[计数];
}
}
document.write(**这是我一直坚持的部分**+'被宣布为赢家);
maximumTotalVoteIndex=totalVotesArray[计数]; 这是主要问题

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

所以您应该保持maximumTotalvoteIndex=count

顺便说一句,在for循环中,计数应该从0开始。

这样试试

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);
var最大值TotalVoteIndex=0;
var maximumVote=totalVotesArray[0];
对于(变量计数=1;计数maximumVote)
{
maximumVote=totalVotesArray[计数];
maximumTotalVoteIndex=计数;
}
}
文件。填写(“得分最高的候选人是”+候选人阵列[maximumTotalVoteIndex]+“有投票”+最大投票权);

在这个循环中,应该是<代码>谢谢亚历克斯——我连领带都没有考虑过!谢谢你的详细回答,谢谢你的解释-我在寻找解释,所以我知道我做的不对。因为我们认为MaimuMultVoTodeIndex为0,我们必须开始从索引1检查。
//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 );