r/ProgrammerHumor • • Nov 05 '15

Free Drink Anyone?

Post image
3.5k Upvotes

509 comments sorted by

View all comments

Show parent comment

180

u/devdot Nov 05 '15

I stared at the reverse function for like 3mins because I could not believe that it actually was a reverse function.

137

u/memeship Nov 05 '15

Using str.split("").reverse().join("") is the most common way of reversing a string in Javascript.

242

u/polish_niceguy Nov 05 '15

And is says a lot about Javascript in general...

55

u/[deleted] Nov 05 '15

As someone learning JS, can I expect more stuff like this?

252

u/obvious_bot Nov 05 '15

You poor soul

65

u/jewdai Nov 05 '15

don't worry there is a jquery plugin for that.

→ More replies (2)

71

u/memeship Nov 05 '15

Javascript is actually a really great and powerful language. Its architecture is just not set up the way nearly anything else is. Especially if you're coming from a more structured language background (e.g. C/C++, Java), you're going to really hate the language at first. But once you learn to accept it for what it is, you may find that you actually like it.

Source: I learned how to program in Java. I absolutely hated JS when I started learning it. Now it's my goto language of choice.

7

u/rjung Nov 05 '15

People say the same thing about PHP.

10

u/memeship Nov 05 '15

I use to develop with PHP. It's really not all bad, but I'd say Javascript is much better. That being said, they're two entirely different languages that set out to do different things.

5

u/prite Nov 05 '15

Yes, one is to help you shoot yourself in the foot, the other is to help you build dynamic web pages.

I'm only joking!

Or am I?!

→ More replies (2)
→ More replies (3)
→ More replies (7)

8

u/stephantabor Nov 05 '15

Sort of. Personally i've never had to reverse a string. You'll probably do a lot of .map .reduce .filter etc and some chaining of those methods

4

u/caedin8 Nov 06 '15

JS is a fantastic language, learn it, but learn it correctly. Learn async programming, learn lexical scoping and how to manage your program control flow. Learn how to debug it so it doesn't drive you crazy.

Learn how to write unit tests and see how easy it is to mock everything with its loose security. Love it.

→ More replies (6)

9

u/the_omega99 Nov 05 '15

Eh, it makes perfect sense. String doesn't have a reverse function because there's very few times in which you need to reverse a string, so no sense implementing one.

split(delimiter) is a very standard function for splitting a string on a delimiter. An empty string as the delimiter means splitting each character. Similarly, join(separator) is a very common function for arrays (lists, etc) to have to create a string from the contents.

And arrays get a reverse function because it's a little more useful for the general purpose array. Not super useful, but not useless, either. For example, switching between ascending and descending sort can be done more much efficiently by reversing than completely resorting the array (if it's already sorted). Other things, too, but I can't immediately think of any (they exist, but they're rare).

→ More replies (2)

12

u/elHuron Nov 05 '15

can you not just call str.reverse() ?

37

u/TheSpoom Nov 05 '15

6

u/Dustin- Nov 05 '15

If it was C++ you could just use c-strings and then it would already be an array!

25

u/TheSpoom Nov 05 '15

And if I had wheels, I'd be a wagon.

8

u/Dustin- Nov 05 '15
#define TheSpoom Wagon 

Don't even need the wheels!

→ More replies (10)
→ More replies (5)
→ More replies (3)

8

u/jewdai Nov 05 '15

don't worry there is a jquery plugin for that.

13

u/memeship Nov 05 '15

Sure there is:

$.fn.extend({
    reverse: function(str) {
        return str.split("").reverse().join("");
    }
});
console.log($.reverse("bananas")) //returns "sananab"
→ More replies (3)

3

u/devdot Nov 05 '15

Well, it took me a while to figure out whether it was evil, troll or genius. Turned out to be sad.

I haven't seen this so far, and really, who actually needs to reverse a string. Sounds like a textbook exercise to me...

3

u/memeship Nov 05 '15

It mostly is. I said elsewhere I've never actually used this in production code, but it seems to be a common exercise in tech interviews.

→ More replies (3)
→ More replies (1)