Skip to content

Instantly share code, notes, and snippets.

@hkasera
Created September 11, 2015 15:48
Show Gist options
  • Save hkasera/16023d65e05a5fc3c813 to your computer and use it in GitHub Desktop.
Save hkasera/16023d65e05a5fc3c813 to your computer and use it in GitHub Desktop.
Validating a pair of braces
var a = "{{{}}}}";
var ts = [];
var mismatch = false;
for (var i = 0; i < a.length; ++i) {
if (a[i] === '{') {
ts.push('{');
}
if (a[i] === '}') {
if (ts.length == 0) {
mismatch = true;
break;
}
ts.pop('{');
}
}
if (ts.length == 0 && !mismatch) {
console.log('match');
} else {
console.log('mismatch');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment