/    Sign up×
Community /Pin to ProfileBookmark

JavaScript program help needed

[B]Hi, I was wondering if someone would be so kind as to help me complete a program I am writing.

The program is designed to allow a user to input the number of candidates electing for secretary, then the names of candidates, then the votes for each candidate.

I think I have most code correct, but the output is showing undefined!

I have been working on this program for 2 days (approx 18hrs) now and am getting very stressed with it, hence turned to some friendly help!?[/B]

<HTML>
<HEAD>
<TITLE></TITLE>

<SCRIPT

language=”JavaScript”
type=”text/javascript”>

/* Program to summarise the results of the election for Secretary.
There were 5 candidates.
*
/
// Declare variables
var totalMembership, numberOfCandidates, totalVote; candidateName; candidateVote;
var candidateNameArray = [];
var candidateVoteArray = [];

var candidateName = new Array(candidateNameArray);
var candidateVote = new Array(candidateVoteArray);
totalVote = 0;
// For each candidate the number of votes cast is entered
// and the running total of votes cast so far is calculated

numberOfCandidates = parseFloat(window.prompt(‘How many candidates do you have? ‘, ”));

numberOfCandidate = new Array(candidateNameArray);
for (var count = 0; count < numberOfCandidates; count = count + 1)
{
numberOfCandidates[count] = numberOfCandidates[count] +
parseFloat(window.prompt(‘Please enter candidates name ‘ + (count + 1)));
}

for (var count = 0; count < numberOfCandidates; count = count + 1)
{
candidateVoteArray[count] = candidateVoteArray[count] +
parseFloat(window.prompt
(‘Please enter the votes cast for candidate ‘ + (count + 1)));
totalVote = totalVote + candidateVoteArray[count];
}
// The total votes cast is written out

document.write(‘Total votes cast: ‘ + totalVote + ‘<BR>’);

/*For each candidate, their total vote and their percentage share of the vote is output*/
for (var count = 0; count < numberOfCandidates; count = count + 1)
{
var percentageVote;
percentageVote = (candidateVoteArray[count] / totalVote) * 100;
percentageVote = Math.round(percentageVote *
100) / 100;
document.write(candidateNameArray[count] +
‘……..votes: ‘ + candidateVoteArray[count] +
‘……..% of total vote: ‘ + percentageVote + ‘<BR>’);
}
/* The winning candidate is declared by determining the array index with the largest vote
(assumes that there is an overall winner and no possibility of a tied ballot) *
/
var maxIndex;
maxIndex = 0;
for (var count = 0; count < candidateNameArray.length; count = count + 1)
if (candidateVoteArray[count] > candidateVoteArray[maxIndex])
{
maxIndex = count;
}
document.write(‘Winner is Candidate ‘ + (maxIndex+1) + ‘ with ‘
+ candidateVoteArray[maxIndex] + ‘ votes’);

</SCRIPT>

</HEAD>

<BODY>
</BODY>

</HTML>

[B]Thank you for looking,
Regards
Jon.[/B]

to post a comment
JavaScript

0Be the first to comment 😎

×

Success!

Help @Jonwheeler spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...