Code is correct, but not cleared the challenge?

Tell us what’s happening:

Your code so far

function reverseString(str) {
  
  for(i=str.length-1;i>=0;i--){
   string = string + str.charAt(i);
  }
  return string;
}

reverseString("Howdy");

Your browser information:

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

Link to the challenge:
https://www.freecodecamp.org/challenges/reverse-a-string

Anybody here can help me?
Actually i have done this reverse challenge but, not yet have passed it, why?

You haven’t defined string and i variables.

I changed your code to this to get it working.

function reverseString(str) {
  var string = "";
  for(var i=str.length-1; i>=0; i--){
   string = string + str.charAt(i);
  }
  return string;
}
1 Like

Thanks…
btw can we define variable outside the function also?
And if we cant’t , why so?

yes, ofcourse. You can read these to understand the difference between global vs local variables.

https://www.freecodecamp.org/challenges/global-scope-and-functions , https://www.freecodecamp.org/challenges/local-scope-and-functions and https://www.freecodecamp.org/challenges/global-vs-local-scope-in-functions

1 Like