I was trying to reverse a string using a for loop. The approach I took was to iterate backwards through the entire string, get each separate character of a string in a descending order and then concatenate the reversed characters together in a new, reversed string. However, I encountered a small issue - the string did reverse, but 'undefined' was concatenated at the start.
What am I doing wrong and why this is hapenning? Many thanks for help in advance.
Please, see the code snippet and console for details.
const string = "A quick brown fox jumped over the lazy dog.";
let newStr = "";
for (let i = string.length; i >= 0; i--) {
let char = string[i];
newStr += char;
}
console.log(newStr);
istarts atstring.length, consider what happens when you try and accessstring[string.length]from your stringstring.split("").reverse().join("")