JSFiddle - React, Tailwind, and code Playground

by IPWright83

JavaScript

function parse(script) {

	let parenthesisDepth = 0;
  let bracketDepth = 0;
  let squareBracketDepth = 0;

	for(let i = 0; i < script.length; i++) {
  	switch(script[i]) {
    	case "[": squareBracketDepth++; break;
      case "(": bracketDepth++; break;
      case "{": parenthesisDepth++; break;
      case "}": parenthesisDepth--; if(parenthesisDepth < 0) return false; break;
      case ")": bracketDepth--; if(bracketDepth < 0) return false; break;
      case "]": squareBracketDepth--; if(squareBracketDepth < 0) return false; break;
      default: break;
    }
  }

  return bracketDepth === 0 && 
  			 parenthesisDepth === 0 && 
         squareBracketDepth === 0;
}

console.log(parse("{}"));
console.log(parse("()"));
console.log(parse("[]"));
console.log(parse("{'()'[][[]]}"));