DEV Community

Discussion on: code every day with me

Collapse
frankwisniewski profile image
Frank Wisniewski
var isValid = function (s) {
  c = true;
  while(c){
    if (s.search(/\(\)|\{\}|\[\]/g) >= 0 )
      s=s.replace(/\(\)|\{\}|\[\]/g,'')
    else 
      c = false 
  }
  return (s.length==0) ? true : false
}
console.log (isValid('[()()[]{}]'))
console.log (isValid('[({)(})[]{}]'))
Enter fullscreen mode Exit fullscreen mode
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