Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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

Daniel Guy
Daniel Guy
14,311 Points

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,824 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.