www.webdeveloper.com
Results 1 to 2 of 2

Thread: Totals for values in an array

  1. #1
    Join Date
    Nov 2004
    Posts
    1

    Question Totals for values in an array

    Apologies to everyone if i've missed something really obvious.

    I have a list of values in an array which have come from a dynamc query. I want to total numbers from one column of the array and display this as the last row of a resulting table.

    Can anyone point me at some code to calculate and store the totals?

  2. #2
    Join Date
    Jan 2003
    Location
    Texas
    Posts
    10,413
    Code:
    <script type="text/javascript"><!--
     function addAll(arr){
       var newArr = 0;
      for(j=0; j<arr.length; j++){
         newArr += parseFloat(arr[j]);
      }
     return newArr;
    }
    
     var arrA = ["12.20", "10.04", "15.19", "20.22"];
     var arrB = ["3.29", "13.39", "28.93", "1.28"];
        document.write('<table><th>Price1</th><th>Price2</th><th>Total</th>');
      for(var i=0; i<arrA.length; i++){
       if(arrA[i+1]){
        document.write('<tr><td>'+arrA[i]+'</td>');
        document.write('<td>'+arrB[i]+'</td><td>');
        document.write(parseFloat(arrA[i]+arrB[i]).toFixed(2));
        document.write('</td></tr>');
       } else {
        document.write('<tr><td>'+addAll(arrA)+'</td>');
        document.write('<td>'+addAll(arrB)+'</td><td>');
        document.write(parseFloat(addAll(arrA)+addAll(arrB)).toFixed(2));
        document.write('</td></tr>');
       }
     }
        document.write('</table>');
    //--></script>
    Visit Slightly Remarkable to see my portfolio, resumé, and consulting rates.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles