Structure values add with Javascript

wayna's picture

Hello Everybody!

I'm posting this topic because i'm quite stuck, due to my lack of skills in javascript. I'm quite shure the thing i want to do might be quite simple but... I have been stuck on it for hours, so i'm coming to you, maybe somebody will know the answer.

Here what i want to do: Let's say i have a numbers structure, is there any way to addition all the members, so the output will be the result of all numbers?

Ex: If my structure is:
0 : "0"= 3
1 : "1"= 4
2 : "2"= 0
3 : "3"= 0

-> My resulting value would be 7

I have found This thread which takes a structure, to return it as a flat structure, which is (i think), not so far from what i want to do.

function (__structure arrOut) main (__structure arrIn, __number n)
{
   var result = new Object();
   var arrNew = new Array();
   var arrTemp= new Array();
   if(!_testMode) {
       for(i=0;i<arrIn.length;i++)
       {
          arrTemp[i]=arrIn[i];
       }
      for(var i = 0; i < arrTemp.length; i += n) {
         arrNew.push(arrTemp.slice(i, i+n).join(" "));
      }
   }
 
   result.arrOut = arrNew;
   return result;
}

But i can't get to change the good parts to make it work, so any help would be highly appreciated!
Many thanks

Serious Cyrus's picture
Re: Structure values add with Javascript

Easy stuff, just google general javascript stuff and think of the __structure as an array.

Might be other ways to do it, i'm no js expert, but this'll do.

function (__number outputNumber) main (__structure inputStructure)
{
   var result = new Object();
   var total  = 0;
   if (inputStructure) {
      for (var i = 0; i < inputStructure.length; i++) {
         total += inputStructure[i]
      }
   } 
   result.outputNumber = total;
   return result;
}

wayna's picture
Re: Structure values add with Javascript

Oh yes many thanks!

I know i need to get into learning javascript tough!

Thank you