# Sum won't add from array

**URL:** <https://forum.makecode.com/t/sum-wont-add-from-array/45391>\
**Category:** Help\
**Tags:** graphics-and-math, extension\
**Created:** [August 13, 2026, 9:30pm UTC](https://forum.makecode.com/t/sum-wont-add-from-array/45391 "2026-08-13T21:30:19Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cybercube](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/cybercube/32/34628_2.png) [@Cybercube](https://forum.makecode.com/u/Cybercube)\
**Post date:** [August 13, 2026, 9:30pm UTC](https://forum.makecode.com/t/sum-wont-add-from-array/45391/1 "2026-08-13T21:30:19Z")

</div>

so, i was make a block for my extension which is the “average of array” block but when i tried to add up the sum it returns NaN. Here’s the code:

```auto
     //% block="Find the average of $array"
    export function AverageOf(array: number[]) {
        let sum: 0
        for (let i = 1; i < array.length; i++) {
            sum += array[i];
            console.log(sum)
        }
        return sum;
    }

```

---

<div class="post-metadata">

**Author:** ![AlexK](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/alexk/32/33339_2.png) [@AlexK](https://forum.makecode.com/u/AlexK)\
**Post date:** [August 14, 2026, 2:14am UTC](https://forum.makecode.com/t/sum-wont-add-from-array/45391/2 "2026-08-14T02:14:39Z")

</div>

```typescript
let sum: number = 0

```

If you want the average, then you want

```typescript
return sum / array.length

```

Also, the first index for arrays is zero (`0`), not one (`1`).

---

<div class="post-metadata">

**Author:** ![CodaKnight](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/codaknight/32/33491_2.png) [@CodaKnight](https://forum.makecode.com/u/CodaKnight)\
**Post date:** [August 14, 2026, 2:23am UTC](https://forum.makecode.com/t/sum-wont-add-from-array/45391/3 "2026-08-14T02:23:23Z")

</div>

You wrote

let sum: 0

Instead of:

let sum: number  
or  
let sum: number = 0

Also there are three other bugs I’m pretty sure in your block:

1. **Missing Division:** You are only adding the numbers, not dividing by the array length to get an actual average.
2. **Skipping the First Element:** Your loop starts at `let i = 1`. JavaScript arrays start at `0`, so you are completely skipping the first item in the list.
3. **Empty Arrays:** If an empty array is passed, dividing by a length of `0` will return `NaN`. [[1](https://www.wscubetech.com/resources/javascript/programs/calculate-average-array)]

I fixed your code so it gets the average of an array.

```typescript
//% block="Find the average of $array"
export function AverageOf(array: number[]): number {
    if (array.length === 0) return 0; // Handle empty array case

    let sum = 0; // Correctly initializes value to 0
    for (let i = 0; i < array.length; i++) { // Starts at index 0
        sum += array[i];
    }
    
    return sum / array.length; // Correctly calculates the average
}

```

---

<div class="post-metadata">

**Author:** ![Cybercube](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/cybercube/32/34628_2.png) [@Cybercube](https://forum.makecode.com/u/Cybercube)\
**Post date:** [August 14, 2026, 5:09pm UTC](https://forum.makecode.com/t/sum-wont-add-from-array/45391/4 "2026-08-14T17:09:01Z")

</div>

Thank you for helping!

---

<div class="post-metadata">

**Author:** ![AlexK](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/alexk/32/33339_2.png) [@AlexK](https://forum.makecode.com/u/AlexK)\
**Post date:** [August 14, 2026, 7:49pm UTC](https://forum.makecode.com/t/sum-wont-add-from-array/45391/5 "2026-08-14T19:49:54Z")

</div>

You also can write the `for` loop without using the index:

```typescript
    for (let element of a) {
        sum += element
    }

```

If you want to be _really_ fancy, you can eliminate the loop and use the `forEach()` method on the array instead.

```typescript
    a.forEach((element: number) => sum += element)

```
