Created
August 28, 2018 04:46
-
-
Save aykutyaman/9b31bb59980b4c37165a07c25995a999 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
// reverse a string using stack data structure | |
const reverse = s => { | |
let stack = []; | |
let reversed = ''; | |
// push all characters of string to stack | |
for (var i = 0; i < s.length; i++) { | |
stack.push(s[i]); | |
} | |
// pop all characters of string and put them into reversed string | |
while (stack.length > 0) { | |
reversed += stack.pop(); | |
} | |
return reversed; | |
} | |
console.log(reverse('aykut') === 'tukya'); | |
console.log(reverse('a b c') === 'c b a'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment