Tell us what’s happening:
Hi campers, i know this challenge seems easy, but I don’t understand what to do. Appreciate if anyone can help point me to where I can read more about Arrays to solve this challenge. Thank you!
**Your code so far**
function reverseString(str) {
let str = [];
for(i = 0; i < 10; i = i + 1){
str = a;
a = a +i;
}
return str;
}
reverseString("hello");
**Your browser information:**
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36
You cannot assign undefined variable i.e a to a variable. Also the parameter str is a keyword that’s why it is highlighted. Also the variable you’re taking as a parameter you’re re-assigning it to an empty array.
I would suggest taking a step back and working on the logic first before diving into code.
The biggest mistake I made when I first started learning was I didn’t have a clear plan to solve the problem.
I would just dive into code and there would be bugs everywhere.
Step away from the code and ask yourself how would you solve this as a human.
What would you do if you had to reverse the word, hello?
You would take the last character o and place it at the end of the new reversed word.
let reversedWord = 'o'
Then you would take the second to last character and place it at the end of the reversed word.
let reversedWord = 'ol'
Repeat that process, working from right to left until you run out of characters and successfully created the new reversed word.
let reversedWord = 'olleh'
Now you need to slowly, turn that logic we just went through and translate that into code.
You can solve this without turning it into an array.
Yes there is a built in reverse method but you can also solve it with just a for loop.
I would first create a variable and assign it an empty string.
Then I would tackle the logic of the for loop.
Remember that we are working from right to left and adding a character from the end of the old string and placing it at the beginning of the new str.
You will need to fix the logic for all three parts of this for loop
Start by fixing this part
Instead of i=0 you want to tell the computer i equals the last character of the string.