Skip to content

Instantly share code, notes, and snippets.

@syxolk
Last active March 31, 2019 14:03
Show Gist options
  • Save syxolk/d3c57e65b5337a0be8799bb1e3ee0ed4 to your computer and use it in GitHub Desktop.
Save syxolk/d3c57e65b5337a0be8799bb1e3ee0ed4 to your computer and use it in GitHub Desktop.
const arr = [1,1,0,0,1,1,1,0,1];
function sumConsecutiveOnes(arr) {
return [...arr, 0].reduce((acc, value) => {
if(value === 1) {
acc.counter += 1;
} else if(acc.counter > 0) {
acc.output.push(acc.counter);
acc.counter = 0;
}
return acc;
}, {
output: [],
counter: 0,
}).output;
}
console.log(sumConsecutiveOnes(arr));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment