Last active
March 31, 2019 14:03
-
-
Save syxolk/d3c57e65b5337a0be8799bb1e3ee0ed4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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