Javascript String backwards()

Description

Javascript String backwards()


String.prototype.backwards = function(){
  var out = '';
  for(var i = this.length-1; i >= 0; i--){
    out += this.substr(i, 1);/*  ww  w  .  jav a2 s .  c  om*/
  }
  return out;
}

Javascript String backwards()

/* reverse a string */
String.prototype.backwards = function() {
    var result = this.split('').reverse().join('');
    return result;
}

var back = 'a man a plan'.backwards();
console.log(back); //nalp a nam a



PreviousNext

Related