Created
May 7, 2021 11:52
-
-
Save basil2style/e29d19be617f5a77a2723139459bb709 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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