Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript DOM Scripting By Example Editing and Filtering Names Filter Invitees Who Have Not Responded

I thought that it may be quite excessive to loop through ul.children twice so wanted to come up with a dryer solution...

filterCheckbox.addEventListener('change', (e) => { const isChecked = e.target.checked; const lis = ul.children; for (let i=0; i < lis.length; i += 1) { let li = lis[i]; if (isChecked) { if (li.className === 'responded') { li.style.display = ''; } else { li.style.display = 'none'; } } else { li.style.display = ''; } } });

This seems to work but I was wondering if there was a reason it wasn't done this way on the tutorial?

2 Answers

Hi, in programming there are often many approach to a problem, so sometimes you might end with a better solution than the teacher!

Erland Van Olmen
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Erland Van Olmen
Full Stack JavaScript Techdegree Graduate 17,825 Points

In theory, at least in a compiled language (which Javascript isn't), your solution would be slower. Not that you would be able to measure the difference ;) In traditional programming languages, one will always try to remove unnecessary checks from loops, especially if these loops are nested. It is best practice if you will, the instructor probably does it out of habit.