Hi guys i am stuck on this challenge
**Your code so far**
function reverseString(str) {
var rev = "";
str.split("");
for(var i = str.length -1; i >= 0; i--){
return str;
}
reverseString("hello");
}
**Your browser information:**
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:97.0) Gecko/20100101 Firefox/97.0
Challenge: Reverse a String
Link to the challenge:
columbuschidozie1:
return str;
What do you expect this line to do?
return str
literally says “return the str
variable”. What is stored in str
?
hello but needs to be reversed to olleh
So return str
can’t possibly work, since it returns “hello”.
so what should i return if not str??
You probably don’t want to return anything inside of the loop. If you have a return
statement inside of the loop, it will only run once.
Where is your logic for actually reversing the order of the letters in the string?
1 Like
Hi @columbuschidozie1 !
I think it would help if you wrote out in english the steps for solving this problem.
Then you can slowly translate that into code.
That approach will help you clean up the logic for your code.
1 Like
you mean split() of reversing string??
split()
doesn’t reverse anything. It splits a string into an array.
Currently, you
Declare a variable rev
as an empty string (don’t use var
! Only use const
or let
)
split()
the str
and throw away the result
Set up a for loop from i = str.length - 1
to i === 0
with i
decreasing. (Again, no var
!)
Inside of the loop, immediately return the original str
That doesn’t reverse anything.
So… what is your plan . Not your code, but your plan . How would you do this, in plain English? What are you trying to make the code do, step by step?
i know this sucks.but honestly speaking i dont know how to do this in plain English
how exactly do i do this???
We aren’t going to tell you exactly how to do this. That does not help you learn how to code at all!
Lets look at this sentence:
The dog jumped over the cat
Which word in this sentence is the longest?
because it has six letters
This sort of intuitive thing that you can just see is exactly what computers don’t understand.
To teach a compute how to do that, you would need to tell it how to split up a sentence and count word lengths. You need to start making small steps the computer can understand.
Same thing here.
What is the reverse of the word: apples
How do you know?
selppa because it was reversed
How did you do that though? Pretend I’ve never reversed a string before in my life. I want to learn how to do that!