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?

Hi,
just a point


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/Core_JavaScript_1.5_Reference/Operators/Special_Operators/in_Operator

Bye.

Thank you to everybody. You helped me to understand how to do it.

@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.

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

Thanks for that :slight_smile:

within the function you can do something like:


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

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.


String.prototype.reverse = function() {
  return this.split('').reverse().join('');
}

var txt = "Hello World!";
alert(txt.reverse()); // alerts "!dlroW olleH"

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

@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:

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 :slight_smile: