# Sum of array elements?

**URL:** <https://forum.kirupa.com/t/sum-of-array-elements/248074>\
**Category:** flash\
**Created:** [January 3, 2008, 3:01pm UTC](https://forum.kirupa.com/t/sum-of-array-elements/248074 "2008-01-03T15:01:10Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![nubz](https://avatars.discourse-cdn.com/v4/letter/n/e79b87/32.png) [@nubz](https://forum.kirupa.com/u/nubz)\
**Post date:** [January 3, 2008, 3:01pm UTC](https://forum.kirupa.com/t/sum-of-array-elements/248074/1 "2008-01-03T15:01:10Z")

</div>

Hi, new to this forum so hi to all

I am stuck when trying to total the elements in an array, I keep getting NaN and cannot for the life of me see what I am doing wrong.

I am using a prototype function:

```auto

Array.prototype.sum = function(){
 var totalSum:Number,a=0;
 while(a<this.length){
  totalSum+=this[a];
  a++;
 }
 return totalSum;
}

var testArray:Array=Array(5,5,5,5,5,5,5,5,5,5);

trace(testArray.sum());

```

this gives me NaN

any help most appreciated!
