adding the values of an array

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...)

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

    Many Thanks

    Jimmy
     
    borojim, Feb 17, 2009 IP
  2. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #2
    try + parseInt(candidateArray) or parseFloat() if necessary

    even though you enter numbers, it treats them as string as they have arrived from a user input that can return anything...
     
    dimitar christoff, Feb 17, 2009 IP
  3. gnp

    gnp Peon

    Messages:
    137
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Additionally to what dimitar said,

    i see that you use the parseFloat in a couple of places but without assigning the returning value anywhere..

    the parseFloat will not alter the variable that you pass it number as text.. it will return that value to be assigned somewhere..

    so
    
    
    parseFloat(candidateNumb);
    //should be 
    candidateNumb  = parseFloat(candidateNumb);
    
    //AND
    
    parseFloat(candidateArray[i].value);
    //should be
    candidateArray[i].value = parseFloat(candidateArray[i].value);
    
    Code (javascript):
    making these two changes would make your code work as expected because you would now be dealing with numbers..

    hth
     
    gnp, Feb 18, 2009 IP