Skip to content

Instantly share code, notes, and snippets.

@syzer
Created May 7, 2015 15:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save syzer/f40ed918cb5102438500 to your computer and use it in GitHub Desktop.
Save syzer/f40ed918cb5102438500 to your computer and use it in GitHub Desktop.
attempt1
function match_brackets(str){
var stack = [],
lookup = {
//key : value pairs
'(' : ')',
'{' : '}',
'[' : ']',
},
_keys = Object.keys(lookup),
//right = lookup.values
_values = Object.keys(lookup).map(function (key) {
return lookup[key];
});
str.split("").forEach(function(char){
if (_keys.indexOf(char) > -1){
stack.push(char)
}
else if( _values.indexOf(char) > -1){
if (stack.length == 0 || (char != lookup[stack[stack.length-1]] )) {
return false
}else{
lookup[stack.pop()]
}
}
})
return (stack.length == 0)
}
//tests
console.log(match_brackets("{}")) //true
console.log(match_brackets("{]")) //false
console.log(match_brackets("{{}}")) //true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment