Quote:
Originally Posted by Old Pedant
Ummm...yes and no.
The final result of both sets of code will be the same!
In the first case, there are repeated assignments to the innerHTML of "posti", but each one simply OVERWRITES the prior one.
So, in the end, ONLY the LAST value of x will be assigned to the innerHTML and the results are thus identical.
The bigger problem, here, is that the variable x is initialized BEFORE the function is invoked. Because of that, each time the function is called, ANOTHER FULL SET of the values from 1 to 5 is appended to x and thus will appear in the innerHTML.
*PROBABLY* the initialization of x needs to be moved INSIDE the function.
|
No, the results are indeed the same but the first one keeps overwriting with the updated innerHTML of
posti while the second code does it only once.
In both cases the result is
0
1
2
3
4
It makes no difference here whether x is declared inside or outside the function.
If you want only the final value of x then delete the red
x =
x + "<sup>" + i + "</sup><br/>";