Last active
January 6, 2019 16:25
-
-
Save chefThomas/1a6ee15d2219ae89c8e639a0676f68b9 to your computer and use it in GitHub Desktop.
Close Parentheses codewars simple string indices
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
// https://www.codewars.com/kata/simple-string-indices | |
function closeParens(str, openParenIndex) { | |
let gate = 1; | |
let closeParenIndex = -1; | |
for(let i = openParenIndex + 1; i <= str.length; i++) { | |
if(str[openParenIndex] != "(") { | |
return closeParenIndex; | |
} else if(str[i] === "(") { | |
++gate; | |
} else if(str[i] === ")") { | |
--gate; | |
if(gate === 0) { | |
closeParenIndex = i; | |
} | |
} | |
} | |
return closeParenIndex; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment