Nodejs String Reverse reverse()

Here you can find the source of reverse()

Method Source Code

String.prototype.reverse = function () {
    "use strict";
    var temp = "",
        i = 0;//w  ww.  j a va 2 s  . co m
    for (i; i < this.length; i = i + 1) {
        temp += this[this.length - 1 - i];
    }
    return temp;
};

Related

  1. reverse()
    String.prototype.reverse = function() {
      return this.split("").reverse().join("");
    
  2. reverse()
    String.prototype.reverse = function () {
        return Array.prototype.reverse.apply(this.split('')).join('');
    };
    console.log('bumblebee'.reverse());
    
  3. reverse()
    let testString = 'hello';
    let expectedString = 'olleh';
    String.prototype.reverse=function(){
      return this.split("").reverse().join("");
    console.log(testString.reverse() === expectedString);
    
  4. reverse()
    String.prototype.reverse = function() {
      var reversed = [];
      for(var i = 0; i < this.length; i++) {
        reversed[this.length - i - 1] = this[i];
      return reversed.join('');
    
  5. reverse()
    String.prototype.reverse = function () {
        var str = this.toCharArray();
        function swap(arrTemp, i, j) {
            var temp = arrTemp[i];
            arrTemp[i] = arrTemp[j];
            arrTemp[j] = temp;
        for (var i = 0, j = this.length - 1; i < j; i++, j--) {
            swap(str, i, j);
    ...
    
  6. reverse()
    String.prototype.reverse = function(){
      var arr = this.toCharArray();
      function swap(arr,a,b){
        var temp = arr[a];
        arr[a] = arr[b];
        arr[b] = temp;
      for(var x=0,y=arr.length-1;  x<y ; x++,y--){
        swap(arr,x,y);
    ...
    
  7. reverse()
    String.prototype.reverse = function () {
      let str = this;
      let result = '';
      for (let i = str.length - 1; i >= 0; i--) {
        result += str.unicodeCharAt(i);
      return result;
    };
    
  8. reverse()
    String.prototype.reverse = function () {
        function swap(arr, a, b) {
            var temp = arr[a];
            arr[a] = arr[b];
            arr[b] = temp;
        var arr = this.toCharArray();
        for (var x = 0, y = arr.length - 1; x < y; x++, y--) {
            swap(arr, x, y);
    ...
    
  9. reverse() // New ES2015
    String.prototype.reverse = function() {     
        "use strict";
        var result = "";
        for(var i = this.length-1; i >= 0; i--){    
            result += this.charAt(i);
        return result;
    module.exports = String;    
    ...