Code is correct, but not cleared the challenge?

Code is correct, but not cleared the challenge?
0

#1

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:

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


#2

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;
}

#3

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


#4

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