Nodejs String Reverse reverse()

Here you can find the source of reverse()

Method Source Code

// reverse a string

function reverseSlow(s) {
  let result = '';
  for (let i = s.length - 1; i >= 0; i--) {
    result += s.charAt(i);/*from w ww  .  j ava 2  s. c om*/
  }
  
  return result;
}

function reverseFast(s) {
  let result = [];
  for (let i = s.length - 1; i >= 0; i--) {
    result.push(s.charAt(i));
  }
  return result.join('');
}

function reverseBuildIn(s) {
  return s.split('').reverse().join('');
}

String.prototype.reverse = function() {
  return this.split('').reverse().join('');
}

console.log('abcd'.reverse());

Related

  1. reverse()
    String.prototype.reverse = function(){
        var dummy = "";
        for (var i = this.length-1; i >= 0; i--) {
            dummy += this[i];
        return dummy;
    
  2. reverse()
    String.prototype.reverse = function(){
        return this.split("").reverse().join("");
    };
    var thing = String("supercalifragilisticexpealidocious");
    thing.reverse();
    
  3. reverse()
    String.prototype.reverse = function () {
      var splitString = this.split('');
      var reverseArray = splitString.reverse();
      var joinArray = reverseArray.join('');
      return joinArray;
    };
    
  4. reverse()
    var str1 = "abcdefghe";
    String.prototype.reverse = function(){
      var temp = "";
      for (var i = this.length-1; i >= 0; i--){
        console.log(this[i]);
        temp = temp + this[i];
      return temp
    function reverse (str) {
        if (str === "") {
            return "";
        } else {
            return reverse(str.substring(1)) + str.charAt(0);
    
  5. reverse()
    String.prototype.reverse = function ()
        var value = '';
        for(var i = this.length - 1; i >= 0; i--)
            value += this.charAt(i);
        return value;
    };
    ...
    
  6. reverse()
    String.prototype.reverse = function () {
        return this.split('').reverse().join('');
    };
    String.prototype.isPalindrome = function () {
        var s = this.toLowerCase().replace(/[^a-z]/g, '');
        return (s.reverse() === s);
    };
    ('A man, a plan, a canoe, pasta, heros, rajahs, ' +
    'a coloratura, maps, snipe, percale, macaroni, ' +
    ...
    
  7. reverse()
    String.prototype.reverse=function(){
      var arr=this.toCharArray();
      return arr.reverse().join("");
    
  8. reverse()
    function reverseWordsInString(str) {
        var words = str.split(' ');
        var resultStr = '';
        for (var w in words) {
            resultStr += words[w].reverse() + ' ';
        return resultStr;
    String.prototype.reverse = function () {
    ...
    
  9. reverse()
    String.prototype.reverse = function () {
        'use strict';
        var s = "",
            i = this.length;
        while (i > 0) {
            s += this.substring(i - 1, i);
            i--;
        return s;
    ...