Confused about this , JS related

Hi all.
I bumped into this. It just seems so basic but I’m very confused.
Here is the code:

let brs = document.createElement('br');
let box = document.getElementById('container');


for(let i=10;i>=0;i--){
		for(let k=i;k>=0;k--){
		   box.append(k + ' ' );
		}
    
		//let brs = document.createElement("br");
		box.appendChild(brs);
   
}

Now this will only work correctly (print a triangle with the space )
when the commented out line is un-commented.

I’m confused isn’t the top (let brs= …) declaration visible from within the nested for loop , it should be global?

Thanks for any explanation.

That’s not what OP is asking.

@agate you’re creating and appending multiple br elements in this scenario, so it needs to go where it’s commented out – you want one per iteration, rather than just one (loop, create element, append it, loop, create element, append it and so on)

1 Like

Thanks, Dan . No still not, but now I get it . Each createElement statement is single use. So it needs a new createElement statement each iteration. Silly newbie mistake :grinning: