Nodejs String Reverse reverseStr()

Here you can find the source of reverseStr()

Method Source Code

//Write a JavaScript function that reverses a string and returns it.

var str = 'Lorem ipsum dolor sit amet.';

String.prototype.reverseStr = function () {
    var string = String(this),
        tempStr = string.split('');
    tempStr = tempStr.reverse();/*w  ww.  j  a  va  2 s .co  m*/
    return tempStr.join('');
};

console.log(str.reverseStr());

Related

  1. 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);
    ...
    
  2. 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;    
    ...
    
  3. 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"));
    
  4. reverse(str)
    String.prototype.reverse = function(str){
        return str.split("").reverse().join("");
        console.log(str)
    var myString = new String();
    myString.reverse("Happy")
    
  5. reverse(string)
    function reverse(s) {
        return s.toString().split("").reverse().join("");
        console.log('from app . js 2');
    
  6. reverseString()
    String.prototype.reverseString = function() {
    var len = this.length, strArray = [], ct = 0;
      for (var i=len-1;i>=0;i--) {
         strArray[ct++] = this.charAt(i);
      return strArray.join("");
    console.log("asdf ajsd".reverseString());
    
  7. reverseString(str)
    function reverseString(str) {
      return str.split("").reverse().join("");
    reverseString("hello");
    
  8. reverseString.prototype.reverse || ()
    String.prototype.reverse = String.prototype.reverse || function() {
        let aux = [],   
            str = this.split('');
        while(str.length > 0) 
            aux.push(str.pop());
        return aux.join('');
    };
    
  9. reverseStringString.prototype.reverseString || reverseString( str )
    const reverseString = ( str ) => {
      return str.split(' ').reverse().join(' ');
    String.prototype.reverseString = String.prototype.reverseString || function reverseString( str ) {
      return str.split(' ').reverse().join(' ');
    module.exports = { reverseString, String };