Last active
          April 14, 2017 02:47 
        
      - 
      
- 
        Save erkobridee/4db195fd142a0c4ce81a271a9f77bcd1 to your computer and use it in GitHub Desktop. 
    check balanced parens using Array.reduce function
  
        
  
    
      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 balancedParens(string){ | |
| return !string.split('').reduce(function(previous, char){ | |
| if(previous < 0) { return previous; } | |
| if(char === '(') { return ++previous; } | |
| if(char === ')'){ return --previous; } | |
| return previous; | |
| }, 0); | |
| } | |
| function checkValue(string){ | |
| console.log(string, ' : ', balancedParens(string)); | |
| } | |
| checkValue('()'); // () : true | |
| checkValue(')('); // )( : false | |
| checkValue('(((())))'); // (((()))) : true | |
| checkValue('())))'); // ()))) : false | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment