SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Addict sorin21us's Avatar
    Join Date
    Mar 2009
    Posts
    279
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to reverse leters in words

    I need a function like reverseChars(str_arg) – this will return a string containing the characters of the string str_arg in reverse order. For example, if reverseChars("javascript") is called, the return value will be "tpircsavaj".

    Can someone give a hand?

  2. #2
    SitePoint Enthusiast
    Join Date
    Apr 2009
    Posts
    73
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    within the function you can do something like:

    Code:
    var myString = "Hello world";
    return myString.split('').reverse().join('');

  3. #3
    SitePoint Evangelist TommiChi's Avatar
    Join Date
    Oct 2008
    Posts
    440
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Another (a bit more long-winded) approach would be:
    function returnReverse(myString) {
    var returnString = '';
    var charPos = myString.length - 1;
    while(charPos > -1) {
    returnString += myString.substr(charPos, 1);
    charPos--;
    }
    return returnString;
    }

  4. #4
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,095
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    What SlickAU and TommiChi said is correct.
    Nota that you can also put that function in the String prototype. That way it's available on every string you create, and you don't need a global function to handle it.

    Code JavaScript:
    String.prototype.reverse = function() {
      return this.split('').reverse().join('');
    }
     
    var txt = "Hello World!";
    alert(txt.reverse()); // alerts "!dlroW olleH"
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  5. #5
    SitePoint Member
    Join Date
    Jun 2010
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    @ScallioXTX

    Nota that you can also put that function in the String prototype. That way it's available on every string you create, and you don't need a global function to handle it.
    That's true, and in most cases is WAY faster (as the method is added to the String prototype, which is used for all String instances)...

    Prototype.js uses this approach heavily, and it's one of my biggest complaints with the library. Native JS types (String, Array, etc.) should be predictable. While this is a fairly innocent usage, I'd argue that it messes with people's understanding of the language, and can make large team-based projects difficult to maintain (because people often get in the habit of extending natives for personal convenience).

    Beyond that, if a browser vendor decides to implement String.reverse(), it will almost certainly be much faster (as is the case with native vs. extended implementations of Array.forEach()). For this reason, I'd recommend to *at least* check for the native method before adding it, so that you can future-proof your code.

    For example:

    Code:
    if (!(reverse in String.prototype)) {
        String.prototype.reverse = function() {
            return this.split("").reverse().join("");
        }
    }
    Of course, how you do the checking is up to you, but that check ensures that if future browsers decide to implement the method, you won't overwrite it with your extension. Hope that helps

  6. #6
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,095
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    You make a very good point keeganwatkins!

    I rarely (if never) use prototypes myself, for the reasons you've just outlined.
    I just use them for these "simple" cases when it comes in handy to add it to the String prototype so you can easily apply it to every string you create.

    PS. Agreed, Prototype.js is not among my favorites either
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  7. #7
    SitePoint Addict sorin21us's Avatar
    Join Date
    Mar 2009
    Posts
    279
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you to everybody. You helped me to understand how to do it.

  8. #8
    SitePoint Member
    Join Date
    Jun 2010
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    @ScallioXTX

    I just use them for these "simple" cases when it comes in handy to add it to the String prototype so you can easily apply it to every string you create.
    totally understandable, and given the nature of the original question, i think your answer was pretty appropriate. i just figured that if you were going to take the time to explain the *how*, i could at least contribute with a brief *why/whynot*.

    cheers.

  9. #9
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,095
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by keeganwatkins View Post
    i just figured that if you were going to take the time to explain the *how*, i could at least contribute with a brief *why/whynot*.
    Thanks for that
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  10. #10
    SitePoint Guru whisher's Avatar
    Join Date
    May 2006
    Location
    Kakiland
    Posts
    732
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by keeganwatkins View Post
    @ScallioXTX

    Code:
    if (!(reverse in String.prototype)) {
        String.prototype.reverse = function() {
            return this.split("").reverse().join("");
        }
    }
    Hi,
    just a point
    Code JavaScript:
    if (!('reverse' in String.prototype)) {
        String.prototype.reverse = function() {
            return this.split("").reverse().join("");
        }
    }

    reverse in this case is a string.

    https://developer.mozilla.org/en/Cor...rs/in_Operator


    Bye.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •