Created
October 16, 2012 12:30
-
-
Save NdYAG/3898976 to your computer and use it in GitHub Desktop.
Reverse a string in Javascript
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
String.prototype.reverse=function(){ | |
var that=this;//or this points to window | |
function rev(len,result){ | |
if(len==0) | |
return result; | |
else{ | |
//console.log(this); | |
return rev(len-1,result+that[len-1]); | |
} | |
} | |
return rev(this.length,""); | |
} | |
'string'.reverse();//'gnirts' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment