DEV Community

Anjali Gurjar
Anjali Gurjar

Posted on

JS Question

Question is balance "{([])}"

function isBalanced(str) {
const obj = {
'{': '}',
'}': '{',
'[': ']',
']': '[',
'(': ')',
')': '('
};

const stack = [];

for (const char of str) {
if (obj[char] === stack[stack.length - 1]) {
stack.pop();
} else {
stack.push(char)
}
}
return !stack.length;
}
console.log(isBalanced('{[]}([{}])'))

Top comments (0)