@grizhlieCodes
Posted
Hi Ervin,
I made a video covering how I did this challenge, a lot of people seem to have found it useful. I'm pretty sure I explain the JS as I'm going through it.
In case you want a sneak peak of the code, here is the link to my GitHub repo.
Generally there is nothing wrong with using a for loop though.
The alternative you can try is the forEach loop.
It works like a normal for loop in a way, it usually takes in an array of items (lets say cards or buttons) and you can just run any code you want.
You have access to the index of each item as well as the item itself so for example:
buttons.forEach((button, index) => {
button.textContent = `Button num ${index + 1}`;
})
Whether or not it uses less code: probably. But I'd concern myself with readability personally rather than length. The less work your future self/someone else has to do to understand the code, the better. I've seen people obsess over quantity of code and they sometimes go as far as to sacrificing readability (I'm not saying you do this!), and man it's tough to work out what's what. Especially in the evening after a bunch of other work π₯²
Happy learning π!
P.S. I go into some CSS that you may find useful in the video for some additional tips.