Need help with loop Task 1

Hi, I’m stuck with the loop test 1. The result only lists the last item in the array, I think possibly it is not creating a new list item each time but writing over the first item hence leaving last item showing only. Not sure where I’m going wrong.

the code; https://jsfiddle.net/nickm49/h2m6re9a/20/

the test; https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Building_blocks/Test_your_skills:_Loops

would appreciate any help,
thanks,

Nick

Hi @nickmaskell

Exactly. Because the line creating the <li> is outside the loop it only gets create once. Then, its content gets replaced every round of the loop.

You need to move one line in your code to make it work correctly. Can you find out which one?

Cheers,
Michael

ahh, thanks, got it. I thought item variable would reference the createElement.
thanks,
Nick

1 Like

The thing is: appendChild() will append new elements, but will move existing elements. Since the <li> was created outside, it was just “moved” to the same position every round.