got a very quick question to y'all as I'm relatively new to JavaScript. This task is actually from the freeCodeCamp course and it's about reversing a string as shown in the title. The question is, why do we include the -1 integer in the str.length line when we can just use the i-- to decrement the string count while reversing the actual string?
It kind of reminds me of what we do when using recursion for counting a set of numbers like (n-1), (n-2), (n-3) etc. Is it the same pattern going on? Or is it because of array's index counting since 0 is the first letter and not 1. Or something else? Thanks :)
function reverseString(str) {
for (var reversedStr = "", i = str.length - 1; i >= 0; i--) {
reversedStr += str[i];
}
return reversedStr;
}