Created
May 12, 2014 07:56
-
-
Save nodech/070c300a49bb37f13188 to your computer and use it in GitHub Desktop.
Valid Braces Or Anything you wish in OPEN_SYMBOLS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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