Reverse a String - Code works but is not accepted as correct?

var reversedString = ["asdf"];

function reverseString(str) {
  reversedString.pop();
  for(var x = str.length; x > -1; x--) {
    reversedString.push(str[x]);
  }
  var returnedString = reversedString.join("");
  return returnedString;
}
reverseString("Greetings from Earth");

This works everytime and gives me the correct reversed string of the word, but for some reason freeCodeCamp still marks it as wrong. What am I doing wrong?

  1. You start the loop at str[str.length] which is one higher than the largest index of the string, so is undefined, so the first value you push is always that.
  2. You use a global variable. So when you run the first time, reversedString becomes [undefined, 'c','b','a']. Next time you run it, say for ‘def’, you pop reversedString so that is [undefined, 'c', 'b'] then you push the new values on so it becomes [undefined, 'c', 'b', undefined, 'f', 'e', 'd'] and so on.