Challenge: Reverse a String

Hello fellow freecodecamp coders,
I tend to revisit my coding on the challenges of the javascript cv and sometimes I get ideas how to get things better. Now i know that there are many of you professionals, so I wanted to ask for your opinions on this.

The challenge: reverse a string

My code:

function reverseString(str) {
  return [...str].reverse().join('')

So what i have used here is the spread operator to split the letters in an array (please correct me if I am wrong) and then applied reverse and join to get the results of reversing a string. Now I have looked into the discussion of the hints and there is one similair solution that uses firstly str.split() and then reverse and join like I do.

My question:
Is it a good idea to use the spread operator […str] instead of str.split() ?

  • Would it make a difference while using large scale datas?

Thank you for your replies!

1 Like

I don’t see why not. It’s a convenient shortcut and I think most pros prefer to use shortcuts rather than spell it out.

But add a semi-colon at the end! I insist on that :slight_smile: I know it’s trendy nowadays not to use the semi-colon. And maybe for a one-liner like this it isn’t necessary. But when looking at a file of code that is hundreds of lines long I need those semi-colons baby.

You would have to do some testing to see. I’d be interested to know what you find.

1 Like

Thank you for your feedback and quick reply, it is much appreciated!

I was thinking the same way, to have the shortest solution, but I am glad to hear that from someone else.

You are absolutely right about the semicolon, normally I use them everywhere as because it is mentioned in the fcc aswell , but Ive forgot it this time. Will have to work on my debugging a bit more.

You would have to do some testing to see. I’d be interested to know what you find.

I dont have currently any specific examples, but I was thinking in a way if there would be any major differences in processed time on the script while using one method or the other. Will think of it, thank you

Ya, I’m an old guy and like to complain about change :slight_smile: Don’t take me too seriously about that.

hahaha well said. But I agree with you, there has to be order if you run thru thousands of lines of code