Last active
January 28, 2017 17:33
-
-
Save guimochila/ecb46d5fbe0ea9540a1549f8922338cc to your computer and use it in GitHub Desktop.
Balancing Parens - How to balance parens using reduce.
This file contains hidden or 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
function balanceParens(string) { | |
return !string.split('').reduce((previous, char) => { | |
if (previous < 0) { return previous; } | |
if (char === '(') { return ++previous; } | |
if (char === ')') { return --previous; } | |
return previous; | |
}, 0); | |
} | |
balanceParens(")((())())()("); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment