@@ -62,6 +62,10 @@ <h1 class="challenges-hero__title">
6262 <!-- Filter Controls -->
6363 < section class ="challenges-filter " aria-labelledby ="filters-heading ">
6464 < div class ="container filter-controls ">
65+ < div class ="filter-group ">
66+
67+ </ div >
68+
6569 < h2 id ="filters-heading " class ="visually-hidden "> Filter Challenges</ h2 >
6670
6771 < div class ="filter-group ">
@@ -88,14 +92,15 @@ <h2 id="filters-heading" class="visually-hidden">Filter Challenges</h2>
8892 </ select >
8993 </ div >
9094
91- < div class ="search-group " style ="margin-left:auto; ">
95+ < div class ="search-group " style ="display: flex; gap: 0.5rem; margin-left: auto; ">
9296 < input
9397 type ="search "
9498 id ="search-challenges "
9599 class ="search-input "
96100 placeholder ="Search challenges... "
97101 aria-label ="Search coding challenges by title or tag "
98102 />
103+ < button id ="clearFilters " class ="btn btn-secondary "> Clear Filters</ button >
99104 </ div >
100105 </ div >
101106 </ section >
@@ -279,6 +284,59 @@ <h4>Community</h4>
279284 localStorage . setItem ( 'theme' , newTheme ) ;
280285 } ) ;
281286 </ script >
287+ < script >
288+ const searchInput = document . getElementById ( "search-challenges" ) ;
289+ const difficultyFilter = document . getElementById ( "difficulty-filter" ) ;
290+ const categoryFilter = document . getElementById ( "category-filter" ) ;
291+ const challengesList = document . getElementById ( "challengesList" ) ;
292+ const noResults = document . getElementById ( "no-results" ) ;
293+
294+ const challengeCards = Array . from ( challengesList . getElementsByClassName ( "challenge-card" ) ) ;
295+
296+ function filterChallenges ( ) {
297+ const searchTerm = searchInput . value . toLowerCase ( ) ;
298+ const difficulty = difficultyFilter . value ;
299+ const category = categoryFilter . value ;
300+
301+ let visibleCount = 0 ;
302+
303+ challengeCards . forEach ( card => {
304+ const title = card . querySelector ( ".challenge-card__title" ) . textContent . toLowerCase ( ) ;
305+ const cardDifficulty = card . dataset . difficulty ;
306+ const cardCategory = card . dataset . category ;
307+
308+ const matchesSearch = title . includes ( searchTerm ) ;
309+ const matchesDifficulty = ! difficulty || cardDifficulty === difficulty ;
310+ const matchesCategory = ! category || cardCategory === category ;
311+
312+ const isVisible = matchesSearch && matchesDifficulty && matchesCategory ;
313+
314+ card . style . display = isVisible ? "block" : "none" ;
315+
316+ if ( isVisible ) visibleCount ++ ;
317+ } ) ;
318+
319+ noResults . style . display = visibleCount === 0 ? "block" : "none" ;
320+ }
321+
322+ // Event listeners for real-time filtering
323+ searchInput . addEventListener ( "input" , filterChallenges ) ;
324+ difficultyFilter . addEventListener ( "change" , filterChallenges ) ;
325+ categoryFilter . addEventListener ( "change" , filterChallenges ) ;
326+ const clearBtn = document . getElementById ( "clearFilters" ) ;
327+
328+ clearBtn . addEventListener ( "click" , ( ) => {
329+ // Reset all filters
330+ searchInput . value = "" ;
331+ difficultyFilter . value = "" ;
332+ categoryFilter . value = "" ;
333+
334+ // Re-run the filtering function to show all challenges again
335+ filterChallenges ( ) ;
336+ } ) ;
337+
338+ </ script >
339+
282340
283341</ body >
284342</ html >
0 commit comments