Reverse a String Help Please

Tell us what’s happening:
I’ve been through the exercise and have tested it in my console and the output seems almost right. I get the reverse of the string, although the console does also show an ‘undefined’ below the reversed string ‘final’. I’m not sure why this is.

On the FCC console, it says ‘newArray is not defined’. Where is my error?

Your code so far


function reverseString(str) {
  newArray = []
  arr = str.split("");
  for (var i = arr.length; i > -1 ;i--) {
    newArray.push(arr[i]);
    
  }
  final = newArray.join('');
  return final
  }


console.log(reverseString("Howdy"));

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.75 Safari/537.36.

Link to the challenge:

this link might help u out

in javascript you need to define your variables using var, let or const

it is because the returned value from console.log() is undefined.
in the console it will show the returned value from the last thing you wrote, so you can just write a function call as last thing of the code.

2 Likes