function testBrackets(input) { let bracketsObj = { '{':'}', '[':']', '(': ')' } let bracketKeys = Object.keys(bracketsObj) const stack = [] for(str of input){ if (bracketKeys.includes(str)){ stack.push(str) } else if (bracketsObj[stack.pop()] !== str){ return false } } return stack.length == 0 }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter