Last active
August 29, 2015 14:15
-
-
Save kjlape/b3bca7d4b50d55a26bd4 to your computer and use it in GitHub Desktop.
Recursive and iterative string reversal in JS... Cuz work is boring.
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
| var reverse = function(str) { | |
| str = str.split('') | |
| var end = str.length - 1 | |
| for (var i = 0; i + 1 <= end - i; ++i) { | |
| var t = str[i] | |
| str[i] = str[end - i] | |
| str[end - i] = t | |
| } | |
| return str.join('') | |
| } | |
| var reverseR = function(str) { | |
| return str.length > 1 ? str.slice(-1) + reverseR(str.slice(1, -1)) + str[0] : str | |
| } | |
| var reverseCheat = function (str) { | |
| return str.split('').reverse().join('') | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment