JSFiddle - React, Tailwind, and code Playground
by Stas Orekhovskiy
JavaScript
function checkStr(str) {
let chars = str.split(''),
stack = [],
open = ['{', '(', '['],
close = ['}', ')', ']'],
closeIndex,
openIndex;
for (let i = 0, len = chars.length; i < len; i++) {
openIndex = open.indexOf(chars[i]);
if (openIndex !== -1) {
stack.push(openIndex);
continue;
}
closeIndex = close.indexOf(chars[i]);
if (closeIndex !== -1) {
openIndex = stack.pop();
if (closeIndex !== openIndex) {
return false;
}
}
}
if (stack.length !== 0) {
return false;
}
return true;
}
console.log(checkStr('text with([])'));
console.log(checkStr('text with ([]))'));
console.log(checkStr('text with ({[]}) '));
console.log(checkStr('text with ({[]} '));