let testFun = (str) => { let container = []; let match = { "}": "{", "]": "[", ")": "(" }; for (x of str.split("")) { if ((x == "{" || x == "[" || x == "(") && container.indexOf(x) < 0) { container.push(x); } else { if (container.some(item => item == match[x])) { container = container.filter(item => item != match[x]) } else { return false; } } } if (container.length == 0) { return true; } return false; } let str = '(){[()]}[]' console.log(testFun(str)); var isValid = function (s) { let map = { '(': -1, ')': 1, '[': -2, ']': 2, '{': -3, '}': 3 }, stack = [] for (let i = 0; i < s.length; i++) { if (map[s[i]] < 0) { stack.push(s[i]) } else { let last = stack.pop() if (map[last] + map[s[i]] !== 0) { return false } } } if (stack.length > 0) { return false } return true }; console.log(isValid('{}'))