Return to Snippet

Revision: 33737
at October 12, 2010 22:40 by adrianparr


Initial Code
function getSumOfArrayRange($array:Array, $indexStart:int, $indexLength:int):Number {
	var rangeTotal:Number = 0;
	if ($indexStart + $indexLength > $array.length) {
		$indexLength = $array.length - $indexStart;
	}
	for (var i:int = 0; i<$indexLength; i++) {
		rangeTotal += Number($array[i+$indexStart]);
	}
	return rangeTotal;
}

var myArray:Array = [10,21,32,43,54,65,76];
var sumOfRange:Number = getSumOfArrayRange(myArray, 1, 3);
trace("sumOfRange: "+sumOfRange);

// OUTPUT
// sumOfRange: 96

// In this example, the following values were added together ...
// 21 + 32 + 43

Initial URL

                                

Initial Description

                                

Initial Title
AS3 Add a range of Values In an Array

Initial Tags
array

Initial Language
ActionScript 3