70 pts.
 How to change undefined names in JavaScript
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>

Software/Hardware used:
ASKED: June 16, 2009  9:40 AM
UPDATED: June 26, 2009  11:18 AM

Answer Wiki:
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
Last Wiki Answer Submitted:  June 26, 2009  11:18 am  by  UsmanAzhar   75 pts.
All Answer Wiki Contributors:  UsmanAzhar   75 pts.
To see all answers submitted to the Answer Wiki: View Answer History.


Discuss This Question:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _