Need help with reversing a string! Plz help!

This is the code I tried and cannot think of anything else to do. :persevere: Does anyone see anything wrong with it? Any help is appreciated!

function reverseString(str) {
  for (var reversedStr = i = str.length - 1; i >= 0; i--) {
    reversedStr += str[i];
  }
  return reversedStr;
} 

You are assigning reversedStr to str.length - 1 when you initialize it. Therefore the reversed string will always start with the length of str.
Try the following.

for(var reversedStr = '', i = str.length - 1; i >= 0; i--){
}
1 Like

@nibble, thank you! It worked!:sweat_smile:

1 Like

I am glad it worked. Make sure you understand why it worked. Happy hacking.

I would suggest you declare reversedStr before the loop. I really see no good reason to declare it where you are right now.