Homeyscript reduce gives strange result

When i try this simple script

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.

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

Convert the values to numbers first:

array1.reduce((partialSum, a) => partialSum + Number(a), 0);
1 Like

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.