Reverse a String, NaN added to beginning

Reverse a String, NaN added to beginning
0

#1

Hello, My code is working the way they want it to, but every time I run it, but adds NaN to beginning, for instance, if I pass “hello” output says NaNelloh

Your code so far

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

reverseString("hello");

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2683.0 Safari/537.36.

Link to the challenge:


#2

I looked to see if anyone solved it similarly, and they did. What I needed to do was change for loop initialized to i= str.length-1 and it worked. position in string starts at 0 so I guess that is why. topic closed.
function reverseString(str) {
var inverted;
for (i= str.length -1; i > -1; i --){
inverted= inverted + str[i];
}
return inverted;
}

reverseString(“hello”);


#3

I think what you need to do is declare inverted =0;
Here’s what i did.

function reverseString(str) {
var o=’’;
for(var i=str.length-1;i>=0;i–){
o+=str[i];
}
return o;
}
reverseString(“sesom”);

Hope it helps.