Posted By

kayue on 12/31/10


Tagged


Versions (?)

flip string but remain readable


 / Published in: JavaScript
 

  1. String.prototype.flip = function()
  2. {
  3. var result = new Array(this.length)
  4.  
  5. var flipTable = {
  6. '\u005B':'\u005D', '\u0061':'\u0250', '\u0063':'\u0254', '\u0065':'\u01DD',
  7. '\u006D':'\u026F', '\u006E':'\u0075', '\u0075':'\u006E', '\u0072':'\u0279',
  8. '\u0074':'\u0287', '\u0076':'\u028C', '\u0077':'\u028D'
  9. }
  10.  
  11. var last = this.length - 1;
  12. for (var i = 0; i <= last; i++) {
  13. var c = this.charAt(i);
  14. var r = undefined;
  15.  
  16. if( i !== 0 ) r = flipTable[c];
  17.  
  18. // console.log("i:"+i+" c:"+c+" last:"+last+" r:"+r);
  19. result[i] = r != undefined ? r : c;
  20. }
  21. return result.join('')
  22. }

Report this snippet  

You need to login to post a comment.