Skip to content

Instantly share code, notes, and snippets.

@mdrmtz
Created June 9, 2016 01:10
Show Gist options
  • Select an option

  • Save mdrmtz/71eb1c91218f704163d7067796e8a4fa to your computer and use it in GitHub Desktop.

Select an option

Save mdrmtz/71eb1c91218f704163d7067796e8a4fa to your computer and use it in GitHub Desktop.
/**
* Implement function verify (text) which checks whether brackets within text are correctly nested.
* You need to consider brackets of three kinds: (), [], <>.
*/
(function() {
function verify(value){
var str = '';
value = value.replace(/[^\(\)\[\]\<\>]/g,'');
while(value.length != str.length) {
str = value;
value = value.replace('()','').replace('[]','').replace('<>','');
}
return !value.length;
}
console.log(verify("---(++++)----")); //true
console.log(verify("")); //true
console.log(verify("before ( middle []) after")); //true
console.log(verify(")(")); //false
console.log(verify("}{")); //true
console.log(verify("<( >)")); //false
console.log(verify("( [ <> () ] <> )"));
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment