Skip to content

Instantly share code, notes, and snippets.

@dre4success
Created October 2, 2017 20:41
Show Gist options
  • Save dre4success/7f7c3524fe86f689d7cc80e583b26458 to your computer and use it in GitHub Desktop.
Save dre4success/7f7c3524fe86f689d7cc80e583b26458 to your computer and use it in GitHub Desktop.
Three ways or reversing a string
// reversing a string with a decrementing forloop
function reverse(str) {
let st = "";
for(let i = str.length - 1; i >= 0; i--) {
st += str[i]
}
return st
}
// through a recursion
function reverse(str) {
if(!arguments.length === 0) {
return 'wtf dude'
}
// base case
if(str.length <= 1) {
return str
}
return reverse(str.substr(1)) + str[0]
}
// with build in functions
const reverse = (str) => {
return str.split('').reverse().join('')
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment