Created
October 2, 2017 20:41
-
-
Save dre4success/7f7c3524fe86f689d7cc80e583b26458 to your computer and use it in GitHub Desktop.
Three ways or reversing a string
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
// 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