DEV Community

Discussion on: code every day with me

Collapse
coderduck profile image
duccanhole Author

Are you use regex? It's cool !

Collapse
frankwisniewski profile image
Frank Wisniewski • Edited on

Yes regex.
You could shorten it to:

var isValid = function (s) {
    while(s.search(/\(\)|\{\}|\[\]/g) >= 0)
        s=s.replace(/\(\)|\{\}|\[\]/g,'') 
    return (s.length==0) ? true : false
}
console.log (isValid('[()()[]{}]'))
console.log (isValid('[({)(})[]{}]'))
Enter fullscreen mode Exit fullscreen mode