AS3 Add a range of Values In an Array


/ Published in: ActionScript 3
Save to your folder(s)



Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.