Validate Brackets. Here is the examples

validate("{}[]()");
//true
validate("{[()]}");
//true
validate("{[}]");
//false ,they r not in right order
validate("{}}");
//false, last '}' is not paired with '{'

Solution

function validate(string) {
    const stack = [];
    const matchingPairs = {
      "}": "{",
      "]": "[",
      ")": "(",
    };

    for (const char of string) {
      if (char === "{" || char === "[" || char === "(") {
        stack.push(char);
      } else if (char === "}" || char === "]" || char === ")") {
        if (stack.length === 0 || stack.pop() !== matchingPairs[char]) {
          return false;
        }
      }
    }
    return stack.length === 0;
  }

  console.log("1", validate("{}[]()"));
  console.log("2", validate("{[()]}"));
  console.log("3", validate("{[}]"));
  console.log("4", validate("{}}"));