I'm trying to do this exercise:
The challenge is to implement a function which adds all together all the consecutive numbers in an array and pushes them into a new array. example: sumConsecutives([1,1,2,1,1,1,1,2,1,1,1]);
// -> [2,2,4,2,3]
My idea is to split the array into an array of arrays. So for the above example: [[1,1],[2],[1,1,1,1],[2],[1,1,1]] then go through and reduce them.
I've tried with a while loop and push into a temp variable which then gets pushed if the next number is not the same, to no avail.
Any ideas on how to achieve this?
var reducer = a => a.reduce((p,c,i,a) => (i === 0 ? p[0] = c : c == a[i-1] ? p[p.length-1] += c : p[p.length] = c,p),[])