Skip to content

Instantly share code, notes, and snippets.

@danicunhac
Last active January 5, 2025 18:44
Show Gist options
  • Save danicunhac/12cbe5a422a88cf336421c9f29e29c8f to your computer and use it in GitHub Desktop.
Save danicunhac/12cbe5a422a88cf336421c9f29e29c8f to your computer and use it in GitHub Desktop.
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