Skip to content

Instantly share code, notes, and snippets.

@guimochila
Last active January 28, 2017 17:33
Show Gist options
  • Save guimochila/ecb46d5fbe0ea9540a1549f8922338cc to your computer and use it in GitHub Desktop.
Save guimochila/ecb46d5fbe0ea9540a1549f8922338cc to your computer and use it in GitHub Desktop.
Balancing Parens - How to balance parens using reduce.
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