function doItAgain() {
var loopCount = 5;
while(loopCount > 0) {
var target = document.getElementById("target");
target.innerHTML = "LoopCount: " + loopCount + "<br>" + target.innerHTML;
console.log("LoopCount is now: " + loopCount);
loopCount = loopCount -1;
}
Here we have a function that executes a loop five times when triggered by a button click event handler.
The question at hand is why the numbers are displayed on the webpage from 1 to 5 when pressed, but in the console they count down from 5 to 1. Changing the position of the sentence within target.innerHTML can reverse the order, but the reason for this behavior remains unclear.