Nodejs String Reverse reverse()

Here you can find the source of reverse()

Method Source Code

/**//from  w ww .j a v  a2s  .  com
 * Question from http://www.thatjsdude.com/interview/js1.html#string_reverse
 * How would you reverse a string in JavaScript?
 */

// attempt 1
function reverseString(str) {
   var rstr = [];

   var splitStr = str.split('');

   for (i = splitStr.length -1; i >= 0; i--) {
      rstr.push(splitStr[i]);
   }

   return rstr.join('');
}

console.log(reverseString('myTest'));


// attempt 2
function reverseStringRecursive(str) {

   if (str.length <= 1) {
      return str;
   }
   var a = str.split('')
   return a.pop() + reverseStringRecursive(a.join(''));
}

// easy way w/ using built in functions
function reverseStringEasy(str) {
   return str.split('').reverse().join('');
}

// add to String primitive wrapper prototype
String.prototype.reverse = function() {
   return this.split('').reverse().join('');
}

Related

  1. reverse()
    String.prototype.reverse = function(){
      var newString = "";
      for (var i = this.length - 1; i >= 0; i--) {
        newString = newString + this[i];
      };
      return newString;
    var name = "freak yeah I\'m writing strings up in hurr!!"
    name.reverse();
    ...
    
  2. reverse()
    String.prototype.reverse = function(){
      var returnedString = string.split('');
      returnedString.reverse();
      var string = this.join('');
    };
    String.prototype.reverse = function(){
      return this.split('').reverse().join('');
    var test = 'Cruncha muncha cruncha muncha fritos in my butt';
    ...
    
  3. reverse()
    String.prototype.reverse = function() {
        var s = this;
        var o = '';
        for (var i = s.length - 1; i >= 0; i--)
            o += s[i];
        return o;
    var fs  = require("fs");
    fs.readFileSync(process.argv[2]).toString().split('\n').forEach(function (line) {
    ...
    
  4. reverse()
    String.prototype.reverse = function() {
      var reverse = '';
      for (var i = this.length - 1; i >= 0; i--){
        reverse += this[i]
      return reverse;
    };
    
  5. reverse()
    String.prototype.reverse = function()
      var s = '';
      var i = this.length;
      while (i > 0) {
        s += this.substring(i-1,i);
        i--;
      return s;
    ...
    
  6. reverse()
    String.prototype.reverse = function() {
      var splitext  = this.split("");
      var revertext = splitext.reverse();
      var reversed  = revertext.join("");
      return reversed;
    };
    
  7. reverse()
    String.prototype.reverse = function() {
      return this.split("").reverse().join("");
    
  8. reverse()
    String.prototype.reverse = function () {
        return Array.prototype.reverse.apply(this.split('')).join('');
    };
    console.log('bumblebee'.reverse());
    
  9. reverse()
    let testString = 'hello';
    let expectedString = 'olleh';
    String.prototype.reverse=function(){
      return this.split("").reverse().join("");
    console.log(testString.reverse() === expectedString);