The task
I asked similar questions here and here.
Given a string of round, curly, and square open and closing brackets, return whether the brackets are balanced (well-formed).
For example, given the string "([])", you should return true.
Given the string "([)]" or "((()", you should return false.
My solution
...is inspired by @200_success' answer. I only made it more concise.
const isBracketBalanced = brc => {
const braces = {
"(": ")",
"[": "]",
"{": "}",
}, expectCloseStack = [];
const bracketIsBalanced = b => braces[b] ?
expectCloseStack.push(braces[b]) :
expectCloseStack.pop() === b;
return [...brc]
.every(bracketIsBalanced) &&
!expectCloseStack.length;
};
console.log(isBracketBalanced("([])[]({})"));