Well as I always say - this is difficult stuff, don’t be too hard on yourself.
I think the great jazz guitarist once said that every great jazz guitarist was a guitarist that struggled with something but didn’t give up.
I had a hard time figuring out loops. I did a lot of practice, and this MDN article helped me to understand what is going on. There are exercises at the end for practice.
It will come.
Welcome to the forum!
You could look into these exercises.
Programming is hard. It’s ok and perfectly normal if it takes time for certain ideas to click in your mind.