const arrSum = (arr) => { return arr.reduce((a, b) => { return a+b }, 0) } const f = (arr) => { // ..... let max = -Infinity for (let i = 0;i < arr.length; i++) { for (let j = i + 1; j < arr.length; j++) { let newVal = arrSum(arr.slice(i, j+1)) max = max < newVal? newVal: max } } return max }