Freecodecamp: Reverse a string - not accepting correct solution

Created on 3 May 2017  路  6Comments  路  Source: freeCodeCamp/freeCodeCamp

Challenge Reverse a String has an issue.
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36.
Please describe how to reproduce this issue, and include links to screenshots if possible.
I can complete the assignment with unshift but wanted to create this function to reuse in the other algorithm challenges. My output seems correct but is not registering as being correct.

My code:


function reverseString(str) {
  var retstr="";
  var l=str.length;
  for (var i=0; i<str.length; i++){
    retstr+=(str[l-i-1]);
  }

  retstr="\""+retstr+"\"";



  return retstr;
}

reverseString("Howdy");

help wanted

Most helpful comment

I think your line
retstr="\""+retstr+"\"";
is the problem. There is no need to add the double quotes around the reversed string.

All 6 comments

i meant split not unshift

I think your line
retstr="\""+retstr+"\"";
is the problem. There is no need to add the double quotes around the reversed string.

+1 @tpoikela
Just remove that line and you're good to go, and close the issue as well! :)

Also, since you've stored str.length in l you can use it in the for loop:

function reverseString(str) {
  var retstr="";
  var l=str.length;
  for (var i=0; i<l; i++){
    retstr+= str[l-i-1]; // no need for brackets here
  }

  return retstr;
}

Yeah, that is not the correct solution @tgardiner88
For reverseString("Howdy");, your function returns ""ydwoH"", when it should return "ydwoH"

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

reverseString("Howdy");

it works now :D

Closing as resolved.

Was this page helpful?
0 / 5 - 0 ratings