SOURCE

const isParensBalanced = (str) => {
  return str.split('').reduce((counter, char) => {
    console.log('counter',counter);
    console.log('char',char);
    if(counter < 0) { //matched ")" before "("
      console.log('2');
      return counter;
    } else if(char === '(') {
      console.log('3');
      return ++counter;
    } else if(char === ')') {
      console.log('1');
      return --counter;
    }  else { //matched some other char
      console.log('4');
      return counter;
    }
    
  }, 0); //<-- starting value of the counter
}

// isParensBalanced('(())') // 0 <-- balanced
// console.log(isParensBalanced('(asdfds)') )//0 <-- balanced
// isParensBalanced('(()') // 1 <-- not balanced
console.log( isParensBalanced(')('));
console 命令行工具 X clear

                    
>
console