Reverse a String!

Tell us what’s happening:

This is my code.My exercise tell me that:
reverseString(“Howdy”) should become “ydwoH”.
reverseString(“Greetings from Earth”) should return “htraE morf sgniteerG”.
What else I should do?
Your code so far


function reverseString(str) {
  return str = "olleh";
}

reverseString("hello");

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-algorithm-scripting/reverse-a-string/

Hi,

You might find these helpful:

https://www.w3schools.com/jsref/jsref_split.asp
https://www.w3schools.com/jsref/jsref_reverse.asp
https://www.w3schools.com/jsref/jsref_join.asp

:slight_smile:

I had read it but again I can’t do it.

Take a look at the links I posted, if you work through them in order, then you will have all the info you need to complete the challenge.

Don’t forget that you are looking for a solution that works for all strings- at the moment yours only works for the string “Hello”.

I read it but it isn’t clear in my brain.Can you help me more.

Hi,

You can’t reverse a string because they are immutable (can’t be changed). You can change an array as they are mutable.

What you need to do is convert the string to an array, reverse it and then convert it back into a string.

You can use those methods (in the links above) to do this (try the examples in the links to see how they work- you can change the code in them to see what happens).

HINTS:

  • the split method will split a string into an array
  • the join method will join an array into a string

Try and see if you can come up with anything and post the code so we can see you what you at.

Hope this helps :slight_smile:

Thank you for help I do it.

Klaudia in this question, like most questions, you need to be able to write a function that works with arbitrary input

You can’t just write the answer to the example and have the right solution, you need to write a function that takes in str and returns the string backwards

This means that you can use your function on any text and it’ll work