Nodejs String Reverse reverseStringString.prototype.reverseString || reverseString( str )

Here you can find the source of reverseStringString.prototype.reverseString || reverseString( str )

Method Source Code

/*// w  ww  .j a  v  a2 s . c  o  m
Part 1: Write a function to reverse the order of the words in a string.
ex: 'Hello World' >> 'World Hello'

Part 2: Place the reverse function directly on the string object
*/

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 };

Related

  1. reverse(string)
    function reverse(s) {
        return s.toString().split("").reverse().join("");
        console.log('from app . js 2');
    
  2. 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());
    
  3. 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());
    
  4. reverseString(str)
    function reverseString(str) {
      return str.split("").reverse().join("");
    reverseString("hello");
    
  5. 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('');
    };
    
  6. reverseWords()
    String.prototype.reverseWords = function() {
      var arr = this.split(" ");
      var s = "";
      while (arr.length>0) {
          var element = arr.pop();
          s += element + " ";
      return s;
    console.log("akjsdhiuao ajsd".reverseString());
    
  7. reverseWords(str)
    String.prototype.reverseWords = function(str) {
      var tokenArray = [];
      tokenArray = this.split(' ');
      return tokenArray.reverse().join(' ');
    
  8. reverser()
    String.prototype.reverser = function() {
      if (!this || this.length < 2) {
        return this;
        return this.split('').reverse().join('');
    
  9. strReverse()
    String.prototype.strReverse = function() {
      var length = this.length,
          strcopy = '';
      for(var i = length-1; i >= 0; i--) {
        strcopy+=this[i]
      return strcopy;
    console.log("  0011".strReverse())
    ...