# Homeyscript reduce gives strange result

**URL:** <https://community.homey.app/t/homeyscript-reduce-gives-strange-result/75657>\
**Category:** Developers\
**Created:** [January 27, 2023, 2:12pm UTC](https://community.homey.app/t/homeyscript-reduce-gives-strange-result/75657 "2023-01-27T14:12:27Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marcel\_Ubels](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/marcel_ubels/32/18860_2.png) [@Marcel\_Ubels](https://community.homey.app/u/Marcel_Ubels)\
**Post date:** [January 27, 2023, 2:12pm UTC](https://community.homey.app/t/homeyscript-reduce-gives-strange-result/75657/1 "2023-01-27T14:12:27Z")

</div>

When i try this simple script

```auto
var array1 = global.get('waterContractPeriodMonth')
console.log('contractPeriodMonth is: ' +array1)
tmp = array1.reduce((partialSum, a) => partialSum + a, 0);
console.log(' counting contractPeriodMonth is: ' +tmp)

```

This is the result:  
contractPeriodMonth is: 3895.00,0,16750,16750,16750,16750,16750,16750,16750,16750,16750,16750  
counting contractPeriodMonth is: 03895.00016750167501675016750167501675016750167501675016750

Using Number(array1) doesn’t work to get a descent result. How can i use reduce to get a descent result.

---

<div class="post-metadata">

**Author:** ![robertklep](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/robertklep/32/160628_2.png) [@robertklep](https://community.homey.app/u/robertklep)\
**Post date:** [January 27, 2023, 2:54pm UTC](https://community.homey.app/t/homeyscript-reduce-gives-strange-result/75657/2 "2023-01-27T14:54:13Z")

</div>

> [@Marcel\_Ubels](#):
>
> How can i use reduce to get a descent result.

`array1` apparently is an array of strings, and addition for strings means concatenation.

Convert the values to numbers first:

```auto
array1.reduce((partialSum, a) => partialSum + Number(a), 0);

```

---

<div class="post-metadata">

**Author:** ![Marcel\_Ubels](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/marcel_ubels/32/18860_2.png) [@Marcel\_Ubels](https://community.homey.app/u/Marcel_Ubels)\
**Post date:** [January 27, 2023, 3:00pm UTC](https://community.homey.app/t/homeyscript-reduce-gives-strange-result/75657/3 "2023-01-27T15:00:08Z")

</div>

> [@robertklep](#):
>
> `array1.reduce((partialSum, a) => partialSum + Number(a), 0);`

thx, it took some time to discover i had a problem with reduce . Most of the time it worked, till today i saw some of the results gave a NAN. Works great, i will move on to the next part of the script.
