Posted By

adrianparr on 10/12/10


Tagged

array add as3 range sum


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

sexyflash
tspitzr


AS3 Add a range of Values In an Array


 / Published in: ActionScript 3
 

  1. function getSumOfArrayRange($array:Array, $indexStart:int, $indexLength:int):Number {
  2. var rangeTotal:Number = 0;
  3. if ($indexStart + $indexLength > $array.length) {
  4. $indexLength = $array.length - $indexStart;
  5. }
  6. for (var i:int = 0; i<$indexLength; i++) {
  7. rangeTotal += Number($array[i+$indexStart]);
  8. }
  9. return rangeTotal;
  10. }
  11.  
  12. var myArray:Array = [10,21,32,43,54,65,76];
  13. var sumOfRange:Number = getSumOfArrayRange(myArray, 1, 3);
  14. trace("sumOfRange: "+sumOfRange);
  15.  
  16. // OUTPUT
  17. // sumOfRange: 96
  18.  
  19. // In this example, the following values were added together ...
  20. // 21 + 32 + 43

Report this snippet  

You need to login to post a comment.