function getMaxSum(arr) { if (!arr.length) return 0; let sum = arr[0]; let max = arr[0]; for(let i = 1; i< arr.length; i++) { sum = Math.max(arr[i], arr[i] + sum); max = Math.max(sum, max); } return max; } const arr = [6,-3, -2, 7, -15, 1, 2, 2]; console.log(getMaxSum(arr));