Nodejs String Reverse reverse() // New ES2015

Here you can find the source of reverse() // New ES2015

Method Source Code

String.prototype.reverse = function() {     // New ES2015
    "use strict";
    var result = "";
    for(var i = this.length-1; i >= 0; i--){    // this: mean String
        result += this.charAt(i);/*from ww  w. ja v a 2 s.  co m*/
    }
    return result;
}

module.exports = String;    // Muon su dung duoc reverse phai export

Related

  1. 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);
    ...
    
  2. reverse()
    String.prototype.reverse = function () {
        "use strict";
        var temp = "",
            i = 0;
        for (i; i < this.length; i = i + 1) {
            temp += this[this.length - 1 - i];
        return temp;
    };
    ...
    
  3. 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);
    ...
    
  4. 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;
    };
    
  5. 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);
    ...
    
  6. reverse() return this.split("").reverse().join(""); }
    String.prototype.reverse = function(){ return this.split("").reverse().join(""); }
    function palindrome(str) { 
       return str == str.reverse(); 
    console.log(palindrome("level"));
    
  7. reverse(str)
    String.prototype.reverse = function(str){
        return str.split("").reverse().join("");
        console.log(str)
    var myString = new String();
    myString.reverse("Happy")
    
  8. reverse(string)
    function reverse(s) {
        return s.toString().split("").reverse().join("");
        console.log('from app . js 2');
    
  9. reverseStr()
    var str = 'Lorem ipsum dolor sit amet.';
    String.prototype.reverseStr = function () {
        var string = String(this),
            tempStr = string.split('');
        tempStr = tempStr.reverse();
        return tempStr.join('');
    };
    console.log(str.reverseStr());