Hi,
I’m trying to work my way through some beginner/intermediate JavaScript challenges as despite learning a lot about JS in the last few months, I never take enough time out of my day to actually code. I find that whilst I know a fair amount about the syntax and a little about what is available to me, I struggle to actually convert an idea into working JavaScript code.
Here I am tasked with reversing a string, seems simple enough… but I’m having an issue. Can anyone please explain why the console prefixes the outputted string with NaN? Apologies if this is something really simple, but I can’t seem to work it out.
function stringRotate(){
var string = ‘javascriptchallenges’;
var newString;
for(var i = string.length; i > -1; i–) {
newString += string[i];
}
console.log(newString);
}
stringRotate();
//Outputs NaNsegnellahctpircsavaj