Skip to content

Instantly share code, notes, and snippets.

@vampaynani
Created May 18, 2018 21:24
Show Gist options
  • Select an option

  • Save vampaynani/110b17a944259f95e0c1f0c62d82df04 to your computer and use it in GitHub Desktop.

Select an option

Save vampaynani/110b17a944259f95e0c1f0c62d82df04 to your computer and use it in GitHub Desktop.
Matching parentheses in an expression recognizing three pairs of brackets: (), [], and {}
function match(str){
var temp = [];
for(var i = 0; i < str.length; i++){
var current = str[i];
var lastChild = temp[temp.length - 1];
console.log(current, lastChild);
if(current === '(' || current === '{' || current === '['){
temp.push(current);
}
if(temp.length > 0){
if(current === ')' && lastChild === '('){
temp.pop();
}
if(current === ']' && lastChild === '['){
temp.pop();
}
if(current === '}' && lastChild === '{'){
temp.pop();
}
} else {
if(current === ')' || current === '}' || current === ']'){
temp.push(current);
}
}
console.log(temp);
}
return temp <= 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment