Skip to content

Instantly share code, notes, and snippets.

@nodech
Created May 12, 2014 07:56
Show Gist options
  • Save nodech/070c300a49bb37f13188 to your computer and use it in GitHub Desktop.
Save nodech/070c300a49bb37f13188 to your computer and use it in GitHub Desktop.
Valid Braces Or Anything you wish in OPEN_SYMBOLS
function validBraces(braces){
var CLOSE_STACK = [],
CLOSE_SYMBOLS = '}])'.split(''),
OPEN_SYMBOLS = { '{' : '}', '[' : ']', '(' : ')' };
for(var i = 0; i < braces.length; i++) {
if (CLOSE_SYMBOLS.indexOf(braces[i]) > -1 && CLOSE_STACK.pop() !== braces[i])
return false;
else
CLOSE_STACK.push(OPEN_SYMBOLS[braces[i]]);
}
return CLOSE_STACK.length === 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment