Skip to content

Instantly share code, notes, and snippets.

@josenaves
Created January 10, 2017 18:03
Show Gist options
  • Save josenaves/d9f82387d5ecc07d0bb739a5cf60b9b4 to your computer and use it in GitHub Desktop.
Save josenaves/d9f82387d5ecc07d0bb739a5cf60b9b4 to your computer and use it in GitHub Desktop.
Balanced parenthesis problem
function balancedParentesis(string) {
return !string.split("").reduce(function(previous, char) {
if (previous < 0) return previous;
if (char === "(") return previous + 1;
if (char === ")") return previous - 1;
return previous;
}, 0);
}
balancedParentesis("))(("); // False
balancedParentesis(")"); // False
balancedParentesis("()"); // True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment