How to change undefined names in JavaScript

70 pts.
Tags:
HTML
JavaScript
Web development
when i run the following program i get a window.prompt for number of candidates, then window.pront for candidates names, then the prompts for number of votes, once this is done everyhing displys fine on the webpage, except the candidates names, each one just says undefined. Where am am i going wrong ? Thankyou * Program to summarise the results of the election for Secretary. There were 5 candidates. */ // Declare variables var totalMembership, numberOfCandidates, totalVote; var candidateNameArray = ['Bloggs', 'Bennett', 'Dehesh', 'Kelly', 'Turner']; var candidateVoteArray = [0, 0, 0, 0, 0]; var numberofCandidates = 5; var nameArray = new Array(5); numberofCandidates = 5; numberofCandidates = window.prompt('enter number of candidates','') numberofCandidates = parseFloat(numberofCandidates); candidateNameArray = window.prompt('enter name of first candidate','') candidateNameArray = window.prompt('enter name of second candidate','') candidateNameArray = window.prompt('enter name of third candidate','') candidateNameArray = window.prompt('enter name of fourth candidate','') candidateNameArray = window.prompt('enter name of fifth candidate','') candidateNameArray = parseFloat(candidateNameArray); var candidateNameArray = new Array(5); // For each candidate the number of votes cast is entered // and the running total of votes cast so far is calculated numberOfCandidates = candidateNameArray.length; totalVote = 0; 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>
ASKED: June 16, 2009  9:40 AM
UPDATED: June 26, 2009  11:18 AM

Answer Wiki

Thanks. We'll let you know when a new response is added.

Hi ,
You are re-intialising the andidateNameArray once again , as

var candidateNameArray = new Array(5);

you are creating 5 new array objects and whose value is not defiend , hence you are getting the value as undefiend.
just run this piece of code be removing that decalaration , ie. var candidateNameArray = new Array(5); from your code. you should be able to get the values populated as expected.

And also you need to capture the input properly, as per your current code you are trying to re-assign the old value with new input candidate name

Here is the solution for your code
Replace below code with
candidateNameArray = window.prompt(‘enter name of first candidate’,”)
candidateNameArray = window.prompt(‘enter name of second candidate’,”)
candidateNameArray = window.prompt(‘enter name of third candidate’,”)
candidateNameArray = window.prompt(‘enter name of fourth candidate’,”)
candidateNameArray = window.prompt(‘enter name of fifth candidate’,”)
candidateNameArray = parseFloat(candidateNameArray);

//use this for loop
for(var i =0;i<numberofCandidates;i++) {
candidateNameArray[i] = window.prompt(‘enter name of candidate’,”)
}

Cheers
Usman

Discuss This Question:  

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following