adding Array Values to find average/make a percentile graph etc

Discussion in 'JavaScript' started by borojim, Feb 17, 2009.

  1. #1
    Hi I have this script:

    <script type="text/javascript">
    //request the number of candidates
    var candidateNumb = prompt("Please Enter How many Candidates there are", "");
    var candidateArray = new Array();
    var totalVotes = 0;
    parseFloat(candidateNumb);
    
    //request the votes given to the candidates in order
    for (i=0; i<=(candidateNumb-1); i++)
    {
    candidateArray[i] = prompt("Please Enter the number of votes for Candidate " + (i + 1), "");
    parseFloat(candidateArray[i].value);
    
    //display what has been given so far....
    document.write("CandidateArray[" + i + "] is " + candidateArray[i] + "<br />");
    document.write("<br />The total Votes so far is " + totalVotes);
    }
    </script>
    Code (markup):
    where its says:
    //display what has been given so far....
    document.write("CandidateArray[" + i + "] is " + candidateArray[i] + "<br />");
    document.write("<br />The total Votes so far is " + totalVotes);
    Code (markup):
    is not part of the script I have used that as a sort of debug, so that I can see that the values are being passed along nicely.

    now when I change that bit of code to:
    
    //display what has been given so far....
    totalVotes = (totalsVotes + candidateArray[i]);
    
    document.write("CandidateArray[" + i + "] is " + candidateArray[i] + "<br />");
    document.write("<br />The total Votes so far is " + totalVotes);
    Code (markup):
    What I am hoping to get is a value of the entire Array added together, but what actually happens is I get a string of the values concacencatencentacatred.... Joined together!
    (sorry I forget to stop spelling it sometimes...)

    Also dont worry about validation I have that sorted and have removed it for simplicity for the time being.

    Once I have a total of the array values I can carry one but...

    Please can someone let me know what I am doing wrong???

    Many Thanks

    Jimmy
     
    borojim, Feb 17, 2009 IP
  2. bprashanth.gi

    bprashanth.gi Active Member

    Messages:
    55
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    93
    #2
    Hi Jimmy,

    Use parse function when adding with totalVotes. It solves the problem.
    <script type="text/javascript">
    //request the number of candidates
    var candidateNumb = prompt("Please Enter How many Candidates there are", "");
    var candidateArray = new Array();
    var totalVotes = 0;
    parseFloat(candidateNumb);
    
    //request the votes given to the candidates in order
    for (i=0; i<=(candidateNumb-1); i++)
    {
    candidateArray[i] = prompt("Please Enter the number of votes for Candidate " + (i + 1), "");
    //parseInt(candidateArray[i].value);
    totalVotes = totalVotes + parseInt(candidateArray[i]);
    //display what has been given so far....
    document.write("<br />CandidateArray[" + i + "] is " + candidateArray[i] + "<br />");
    document.write("The total Votes so far is " + totalVotes);
    }
    </script>
    Code (markup):
     
    bprashanth.gi, Feb 17, 2009 IP