Last active
January 5, 2025 18:44
-
-
Save danicunhac/12cbe5a422a88cf336421c9f29e29c8f 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(initialStack = []) { | |
this.stack = initialStack; | |
} | |
get() { | |
return this.stack; | |
} | |
pop() { | |
return this.stack.pop(); | |
} | |
push(num) { | |
return this.stack.push(num); | |
} | |
} | |
const reverseString = (str) => { | |
const stack = new Stack(str.split("")); | |
const reversedStack = new Stack(); | |
while (stack.get().length > 0) { | |
reversedStack.push(stack.pop()); | |
} | |
return reversedStack.get().join(""); | |
}; | |
console.log(reverseString("alice")); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment