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.

Revisions

  1. josenaves created this gist Jan 10, 2017.
    14 changes: 14 additions & 0 deletions balanced-parenthesis.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    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