Working through a codewars challenge (Simple Fun #237: Suffix Sums), and while I'm passing all the tests it's giving me a time-out error. The challenge is to make a new array from a given one, where each index of the new array is the summation of that same index in the original to the end of the original array.
For an array 1, 2, 3, -6, the output should be 0, -1, -3, -6.
b[0]= 1 + 2 + 3 - 6 = 0
b[1]= 2 + 3 - 6 = -1
b[2]= 3 - 6 = -3
b[3]= - 6 = -6
My code is this
function suffixSums(a) {
var res=[]
for(i=0;i<a.length;i++){
var newarray=a.slice([i])
res.push(newarray.reduce(function(acc, val){ return acc + val },0))
}
return res
}
Any clue what's up? I'm just learning still obviously, optimization is a whole new world for me