Skip to content

Instantly share code, notes, and snippets.

@curiouslychase
Last active December 25, 2015 16:49
Show Gist options
  • Save curiouslychase/7009098 to your computer and use it in GitHub Desktop.
Save curiouslychase/7009098 to your computer and use it in GitHub Desktop.
Here's the solution I came up with for this interview question on interviewcake.com: "I like parentheticals (a lot). "Sometimes (when I nest them (my parentheticals) too much (like this (and this))) they get confusing." Write a function that, given a sentence like the above, along with the position of an opening parenthesis, finds the correspond…
// I enjoy doing interview questions just because they make my brain feel better.
// Here's the solution I came up with for this interview question on interviewcake.com
// http://www.interviewcake.com/question/matching-parens
var str = 'Sometimes (when I nest them (my parentheticals) too much (like this (and this))) they get confusing.';
findMyMate(str, '(', ')', 10);
/**
* Find the mate of a given character
* @param {str} sentence The string we want to search in
* @param {str} character What we want to match
* @param {str} mate What we want to match against
* @param {int} place The place of the character to start with
*
*/
function findMyMate(sentence, character, mate, place) {
var i = 0,
len = sentence.length,
search = '';
for(len; place < len; place++) {
search += sentence[place];
if(sentence[place] === character) {
i++;
} else if(sentence[place] === mate) {
i--;
}
if(i === 0) {
console.log(place);
return;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment