Created
August 27, 2015 17:19
-
-
Save sergiorgiraldo/55d80d4a1464c233da28 to your computer and use it in GitHub Desktop.
This file contains 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
<script type="text/javascript"> | |
function balancedParens(expr){ | |
var tokens={ | |
"(":")", | |
"[":"]", | |
"{":"}" }; | |
var beginners=[]; | |
var finishers=[ | |
")", | |
"]", | |
"}"]; | |
for (var i=0;i<expr.length;i++){ | |
var chr=expr[i]; | |
if (tokens[chr]){ | |
beginners.Add(chr); | |
} | |
else if (finishers.Has(chr)) | |
{ | |
if (tokens[beginners.GetLast()] != chr){ | |
return false; | |
} | |
} | |
} | |
return beginners.length == 0; | |
} | |
Array.prototype.Has=function(what){ | |
return this.indexOf(what) != -1; | |
}; | |
Array.prototype.Add=function(what){ | |
return this.push(what); | |
}; | |
Array.prototype.GetLast=function(what){ | |
return this.pop(); | |
}; | |
function assert(what, expected){ | |
if (what != expected){ | |
throw new Error(what + " is different from " + expected); | |
alert("error!!"); | |
} | |
} | |
assert(balancedParens("("), false); | |
assert(balancedParens("()"), true); | |
assert(balancedParens(")("), false); | |
assert(balancedParens("(())"), true); | |
assert(balancedParens("[](){}"), true); | |
assert(balancedParens("[({})]"), true); | |
assert(balancedParens("[(]{)}"), false); | |
assert(balancedParens("var supYo = { hey: dog() }"), true); | |
assert(balancedParens("var fiddle = function() { doggy.eat();"), false); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment