I’m trying to figure out the launch countdown active learning puzzle in the tutorial docs.
(https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Building_blocks/Looping_code)
Here’s the solution it gives me:
const output = document.querySelector(’.output’);
output.innerHTML = ‘’;
let i = 10;
while(i >= 0) {
let para = document.createElement(‘p’);
if(i === 10) {
para.textContent = 'Countdown ’ + i;
} else if(i === 0) {
para.textContent = ‘Blast off!’;
} else {
para.textContent = i;
}
output.appendChild(para);
i–;
}
My question is, if I move “let para = document.createElement(‘p’);” above and out of the loop, it just displays “Blast Off!”
But I’m not sure why that’s happening.
In my head, once the para variable is defined, the loop will just write over it with para.textContent, and that will be appended to output with each loop. I’m not sure why it has to be defined within the loop?