-3

I want to write my own method which reverses a string. myStr.reverseString should return it reversed. I don't want to use parenthesis at the end of my command like myStr.reverseString()

String.prototype.reverseString(){ // This line need to be changed
  return String.split("").reverse().join(""); // This line need to be changed
}

let myStr = "Hello World!"
console.log(myStr.reverseString); // Should expected "!dlroW olleH"
Nick Parsons
  • 45,728
  • 6
  • 46
  • 64
Kaan G
  • 219
  • 4
  • 13
  • I don't want to use paranthesis at the end. – Kaan G Jun 29 '19 at 13:09
  • 3
    `I don't want to use paranthesis at the end of my command like myStr.reverseString()` - May I ask why? It seems to me that only serves to confuse the heck out of people. – Tyler Roper Jun 29 '19 at 13:10
  • 2
    You can define a getter property on the `String.prototype`, but it's generally considered a bad practice to mess with built-in prototypes: `Object.defineProperty(String.prototype, 'reverseString', { get() { return this.split('').reverse().join(''); } })` – Christian C. Salvadó Jun 29 '19 at 13:10
  • 2
    If you don't want to call it with `()`, it's not a method. You're looking for a getter property. – Bergi Jun 29 '19 at 13:19
  • You could call it using \`\` like so `myStr.reverseString\`\``, that way you won't need the `()`. You would, of course, need to fix the other issues with your code (String to `this` and use of the `function` keyword) – Nick Parsons Jun 29 '19 at 13:27
  • For other people reading this question please never do this, that lack of parenthesis is a ruby thing that shouldn't exist in JavaScript because it should be clear when a function is being executed. – Ivan Castellanos Jun 29 '19 at 13:32

2 Answers2

2

I think you can accomplish this by using defineProperty.

In order to not use the (), you need to define a getter on String.protoype.

Your code should follow the steps bellow:

  1. Split()
  2. Reverse()
  3. join()

In the end, you will have something like this:

    Object.defineProperty(String.prototype, 'reverseString', { get() { return this.split('').reverse().join(''); } })

    let myStr = "Hello World!" 

    console.log(myStr.reverseString); // Should expected "!dlroW olleH"

[Update] Although this is the answer to this question, be careful while doing this. Please read: Why is extending native objects a bad practice?

[Update 2] As @Ivan Castellanos said, this method won't work for complex strings like: ‍‍‍., if you need to to this, please read: https://stackoverflow.com/a/16776621/800817

  • This doesn't work for complex strings such as one containing the family emoji Just try '‍‍‍'.reverseString, instead of getting the same emoji you get "��‍��‍��‍��" – Ivan Castellanos Jun 29 '19 at 13:35
  • You are right! It doesn't. Please post an answer that can handle it so we can learn this. Thank you! I can update my answer and mention you. – Guilherme Assemany Jun 29 '19 at 13:37
  • It has been answered before, check it here: https://stackoverflow.com/a/16776621/800817 ,TLDR: UTF-16 Surrogate pairs represent a single character, while .split("") breaks by code-point so inversing such code-points creates an error – Ivan Castellanos Jun 29 '19 at 13:45
-1

Okay Try this, this will help you.

 Object.defineProperty(String.prototype, 'reverse', { get() { 
      // Step 1. Use the split() method to return a new array
    var splitString = this.split(""); // var splitString = "hello".split("");
    // ["h", "e", "l", "l", "o"]
 
    // Step 2. Use the reverse() method to reverse the new created array
    var reverseArray = splitString.reverse(); // var reverseArray = ["h", "e", "l", "l", "o"].reverse();
    // ["o", "l", "l", "e", "h"]
 
    // Step 3. Use the join() method to join all elements of the array into a string
    var joinArray = reverseArray.join(""); // var joinArray = ["o", "l", "l", "e", "h"].join("");
    // "olleh"
    
    //Step 4. Return the reversed string
    return joinArray; } })



var x = 'Friday call peter parker';
    console.log(x.reverse);
Dupinder Singh
  • 7,175
  • 6
  • 37
  • 61
  • 1
    "_I don't want to use parenthesis at the end of my command like `myStr.reverseString()`_" – Ivar Jun 29 '19 at 13:26
  • 1
    but why, whats the difference, with or without parentheses?? – Dupinder Singh Jun 29 '19 at 15:10
  • 1
    [Someone already asked that](https://stackoverflow.com/questions/56817784/how-to-write-my-own-method-with-string-prototype#comment100188828_56817784), but OP didn't provide an answer. But that doens't mean that you should give an answer to what OP explicitly didn't ask. – Ivar Jun 29 '19 at 15:13