Skip to content

Instantly share code, notes, and snippets.

@chefThomas
Last active January 6, 2019 16:25
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 chefThomas/1a6ee15d2219ae89c8e639a0676f68b9 to your computer and use it in GitHub Desktop.
Save chefThomas/1a6ee15d2219ae89c8e639a0676f68b9 to your computer and use it in GitHub Desktop.
Close Parentheses codewars simple string indices
// 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