for ( *statement 1* ; *statement 2* ; *statement 3* ) {
// *code block to be executed*
}
Statement 1 is executed (one time) before the execution of the code block.
Statement 2 defines the condition for executing the code block.
Statement 3 is executed (every time) after the code block has been executed.
so statement 1 is ran once at the very start of the for loop, its initiating a variable ‘i’ with the value str.length-1, which is the index of the last letter of the string.
now it runs what’s inside the block, so it adds the last letter of the str to newStr.
statement 3 is then ran, so we subtract 1 from i, giving us the index of the 2nd last letter of the string.
then statement 2 is ran, if it’s true, the loop continues, if it’s false we exit the loop.
so this loop will run until i=0, the index of the first letter of the string.
basically, it counts down from the index of the last letter. each time adding that letter to a new string.
The for loop is running from backwards. This means from the last character to the first character. And this way it reverse a string. Hope you understand what I mean.