Tell us what’s happening:
I have read the split,reverse and join methods but if i wanted to do it with a for loop then what is wrong with my function?
It’s returning “NaNolleh” , not “olleh” . If string “hello” is given.
Also if i initiate my i counter with
i = str.length - 1 ,
then the output changes and gives me “Undefinedolleh”.
I would like some help here please to explain this.
Your code so far
function reverseString(str) {
let reverse;
for(let i=str.length; i>=0; i--){
reverse=reverse + str[i];
}
console.log(reverse)
return reverse;
}
reverseString("hello");
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36
.
Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-algorithm-scripting/reverse-a-string