Array summing function

Discussion in 'JavaScript' started by natashenka_66, Jul 1, 2007.

  1. #1
    Hello! Need help with writing a function to check array indexes and add together all values for the same index.
    Only values with same indexes. I'm creating an array with project numbers as indexes and sum of the project cost as a value. But some projects have sub-projects with same project number (same index) but different cost (different value). So an array will have some of the indexes the same and some different. And i need to sum all values with same indexes. And i can use Only java script.
    Well, actually i didn't progress what far... A bit lost... It's not "Associative" arrays. In fact it's part of BIRT report design, so it's a bit difficult to explain exactly...
    So far I've got:
    summry_bal= new Array()

    function arr_summry_bal()
    {
    t_sum=0;

    for (var loop = 0; loop < summry_bal.length; loop++) {
    if (t_sum == null)
    {t_sum=0;
    t_sum=
    }

    }

    }
    Please, really hope to get some help!!!
    Thank you! :confused: :D
     
    natashenka_66, Jul 1, 2007 IP
  2. Jamie18

    Jamie18 Peon

    Messages:
    201
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    i haven't read too far into your problem but i'm gonna throw something up here for you since you haven't gotten any replies..

    i'm just going to give some pseudocode that might help give you a start, i don't spend all that much time with javascript so if i tried to do it in javascript chances are the syntax could be pretty bad

    for each different index number in the array, indexed with i
        create a variable 'sumOf' concatenated with i, set variable = 0
        for each element of the array
          if arrayElement.index = i
              sumOfi = sumOfi + arrayElement.value
    
    Code (markup):
    and at the end you would have a different variable named sumOf(indexValue) storing the sum for each different index, also instead of worrying about concatenating the variable and the index you could make an array before the loop that stores the greatest index number + 1 elements.. and use sumOf to hold the sum for each index

    hope this is of some help.. sorry i just quickly fired off a reply, hopefully what i wrote makes sense
     
    Jamie18, Jul 9, 2007 IP