Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6072921
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:12:48+00:00 2026-05-23T10:12:48+00:00

I have a simple JavaScript program to calculate the winner of an election. I

  • 0

I have a simple JavaScript program to calculate the winner of an election.
I use a FOR loop to assign the total of each candidates votes into a new array called totalVotesArray, I then output the corresponding candidates names and total votes into the browser window.

I then need to output the highest scoring candidate – I’ve used another FOR loop but I’m not sure I’ve done that part correctly. I’m stuck finding the index of the winning score and relating that the corresponding position in the parallel array.

Anyone know how I do this?

<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>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T10:12:48+00:00Added an answer on May 23, 2026 at 10:12 am

    try like this

    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);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following (fairly) simple JavaScript snippet that I have wired into Greasemonkey.
I have a simple Javascript program that displays a small rectangle in a canvas.
I have a simple javascript program that's acting a little funny. When I try
I have a simple JavaScript Array object containing a few numbers. [267, 306, 108]
I have a simple JavaScript file that has three jQuery $document.ready functions. All three
I have a simple JavaScript game that sends a score to PHP by adding
let's say I have a simple javascript like so: $(document).ready(function(){ if(login == 1) {
I have the following very simple Javascript-compatible regular expression: <script type=text/javascript id=(.+) src=([^]+)> I
I have a simple question concerning Javascript. I am trying to print in a
I have simple HTML code with some JavaScript. It looks like: <html> <head> <script

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.