Created
June 26, 2017 16:04
-
-
Save dakotabryant/78c9d5f19c1b405bffd37683df46ba19 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
function isBalanced(string) { | |
let stack = []; | |
let character; | |
let parenthesesPos; | |
for (var i = 0; character = string[i]; i++) { | |
parenthesesPos = parentheses.indexOf(character); | |
if(parenthesesPos === -1) { | |
continue; | |
} | |
if(parenthesesPos % 2 === 0) { | |
stack.push(parenthesesPos + 1); | |
} | |
else { | |
if(stack.length === 0 || stack.pop() !== parenthesesPos) { | |
return false | |
} | |
} | |
} | |
return stack.length === 0; | |
} | |
console.log(isBalanced('[]{}()')); | |
console.log(isBalanced('[]{}{[]()}')); | |
console.log(isBalanced('[(])')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment