Skip to content

Instantly share code, notes, and snippets.

@basil2style
Created May 7, 2021 11:52
Show Gist options
  • Save basil2style/e29d19be617f5a77a2723139459bb709 to your computer and use it in GitHub Desktop.
Save basil2style/e29d19be617f5a77a2723139459bb709 to your computer and use it in GitHub Desktop.
class Stack {
constructor() {
this.data = [];
}
push(val) {
this.data.push(val);
console.log(this.data)
}
pop() {
if (this.data.length == 0) return 'Underflow';
return this.data.pop();
}
peek() {
return this.data[this.data.length - 1];
}
isEmpty(){
return (this.data.length>0)?true:false
}
}
function bracketSelector(word, openingIndex) {
const stack = new Stack();
let endingIndex = openingIndex;
for (let i = openingIndex; i < word.length; i++) {
if (word[i] === '(') stack.push(word[i]);
else if (word[i] === ')') {
if (stack.peek() === '(') {
stack.pop();
endingIndex = i;
}
}
}
return endingIndex;
}
let word = 'Some ( and okay()) oijo';
let openingIndex = 5;
console.log(bracketSelector(word, openingIndex));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment