Skip to content

Instantly share code, notes, and snippets.

@JesterMan
Created March 17, 2014 19:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JesterMan/9605985 to your computer and use it in GitHub Desktop.
Save JesterMan/9605985 to your computer and use it in GitHub Desktop.
<head></head>
<body>
<script>
var expressions = [ ")(){}", "[]({})", "([])", "{()[]}", "([)]" ];
function expTest(){
var brackTest = function(x, z){
for (var k = 1 +z; k < testInput.length; k++) {
if (testInput[k] == x){
testInput.splice(z,1);
k--;
testInput.splice(k,1);
formatScore--;
j=-1;
k=k-z;
// on the fourth element in expressions (i.e. "{()[]}"), when it is trimmed down to "{[]}", the k=k-z line does not appear to work. k = 1, z=1, but at the start of the next iteration, k=2
return;
}
else if (testInput[k] =='{'){
brackTest('}', k);
}
else if (testInput[k]=='['){
brackTest(']', k);
}
else if (testInput[k]=='('){
brackTest(')', k);
}
else {
debugger
console.log('0'+'\n')
k = testInput.length,
j = testInput.length;
}
};
};
for (var i =0; i < expressions.length; i++) {
var formatScore = expressions[i].length/2;
var testInput = expressions[i].split('');
if (testInput.length%2 == 1){
console.log('0'+'\n');
} else {
for (var j=0; j < testInput.length; j++){
if (testInput[j] =='{'){
brackTest('}', j);
}else if (testInput[j]=='['){
brackTest(']', j);
}else if (testInput[j]=='('){
brackTest(')', j);
}
if (testInput.length==0){
if (formatScore==0){
console.log('1'+'\n');
}else{
debugger
console.log('0'+'\n');
}
}
}
};
};
};
expTest();
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment