Skip to content

Instantly share code, notes, and snippets.

@cviebrock
Created June 30, 2011 03:21
Show Gist options
  • Save cviebrock/1055561 to your computer and use it in GitHub Desktop.
Save cviebrock/1055561 to your computer and use it in GitHub Desktop.
String.reverse and String.translate
String.prototype.reverse = function() {
var str='';
for (i=this.length-1;i>=0;i--) {
str += this.charAt(i);
}
return str;
}
String.prototype.translate = function(from,to) {
var sl = this.length,
tl = to.length,
xlat = new Array(),
str = '';
if (sl<1 || tl<1) return this;
for (i=0; i<256; xlat[i]=i, i++);
for (i=0; i<tl; i++) {
xlat[ from.charCodeAt(i) ] = to.charCodeAt(i);
}
for (i=0; i<sl; i++) {
str += String.fromCharCode( xlat[ this.charCodeAt(i) ] );
}
return str;
}
var msg = 'Hello world!';
alert(msg.reverse());
alert(msg.translate('lor','gis'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment