Skip to content

Instantly share code, notes, and snippets.

@LiuuY
Created July 29, 2016 07:42
Show Gist options
  • Save LiuuY/162a478ef6463a8eb4af327ec15382ab to your computer and use it in GitHub Desktop.
Save LiuuY/162a478ef6463a8eb4af327ec15382ab to your computer and use it in GitHub Desktop.
Valid Braces
function validBraces(braces){
var matches = { '(':')', '{':'}', '[':']' };
var stack = [];
var currentChar;
for (var i = 0; i < braces.length; i++) {
currentChar = braces[i];
if (matches[currentChar]) { // opening braces
stack.push(currentChar);
} else { // closing braces
if (currentChar !== matches[stack.pop()]) {
return false;
}
}
}
return stack.length === 0; // any unclosed braces left?
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment