Created
March 28, 2020 12:50
-
-
Save emmabostian/b9159bb489dde73b79a436944facce02 to your computer and use it in GitHub Desktop.
This file contains 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
export default class Stack { | |
constructor() { | |
this.stack = []; | |
} | |
push(item) { | |
this.stack.push(item); | |
} | |
pop() { | |
return this.stack.pop(); | |
} | |
length() { | |
return this.stack.length; | |
} | |
peek() { | |
return this.stack[this.stack.length - 1]; | |
} | |
print() { | |
return this.stack.join(" "); | |
} | |
isEmpty() { | |
return this.length() === 0; | |
} | |
} | |
import Stack from "./stack"; | |
function reverseAString(str) { | |
let letters = str.split(""); | |
let stack = new Stack(); | |
let reversedLetters = []; | |
letters.forEach(letter => stack.push(letter)); | |
while (!stack.isEmpty()) { | |
reversedLetters.push(stack.pop()); | |
} | |
return reversedLetters.join(""); | |
} | |
console.log(reverseAString("abcdef")); | |
OR just return | |
str.split('').reverse().join(''); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I was thinking in other ways. 😊