@@ -12,10 +12,33 @@ import { modules, students, mentors, classes } from "./hyf.js";
1212 * [{ name: 'John', role: 'student' }, { name: 'Mary', role: 'mentor' }]
1313 */
1414const getPeopleOfClass = ( className ) => {
15- // TODO complete this function
15+
16+ // Find the class with the specified name
17+ const currentClass = classes . find ( ( classInfo ) => classInfo . name === className ) ;
18+
19+ // Check if the class exists
20+ if ( ! currentClass ) {
21+ return 'Class not found' ;
22+ }
23+
24+ // Get students in the class
25+ const studentsInClass = students
26+ . filter ( ( student ) => student . class === className )
27+ . map ( ( student ) => ( { name : student . name , role : 'student' } ) ) ;
28+
29+ // Get mentors in the class
30+ const mentorsInClass = mentors
31+ . filter (
32+ ( mentor ) =>
33+ mentor . nowTeaching === className ||
34+ ( mentor . nowTeaching === currentClass . currentModule )
35+ )
36+ . map ( ( mentor ) => ( { name : mentor . name , role : 'mentor' } ) ) ;
37+
38+ // Combine and return the array of names and roles
39+ return [ ...studentsInClass , ...mentorsInClass ] ;
1640} ;
17- // You can uncomment out this line to try your function
18- // console.log(getPeopleOfClass('class34'));
41+ console . log ( getPeopleOfClass ( 'class34' ) ) ;
1942
2043/**
2144 * We would like to have a complete overview of the current active classes.
@@ -30,7 +53,34 @@ const getPeopleOfClass = (className) => {
3053 * }
3154 */
3255const getActiveClasses = ( ) => {
33- // TODO complete this function
56+ const activeClasses = classes . filter ( ( cls ) => cls . active ) ;
57+
58+ const result = { } ;
59+
60+ activeClasses . forEach ( ( classInfo ) => {
61+ const { name : className , currentModule } = classInfo ;
62+ const peopleInClass = [ ] ;
63+
64+ // Add students in the class
65+ students . forEach ( ( student ) => {
66+ if ( student . class === className ) {
67+ peopleInClass . push ( { name : student . name , role : 'student' } ) ;
68+ }
69+ } ) ;
70+
71+ // Add mentors in the class
72+ mentors . forEach ( ( mentor ) => {
73+ if ( mentor . canTeach && mentor . canTeach . includes ( currentModule ) ) {
74+ peopleInClass . push ( { name : mentor . name , role : 'mentor' } ) ;
75+ }
76+ } ) ;
77+
78+ // Add the array of people to the result object with the class name as the property
79+ result [ className ] = peopleInClass ;
80+ } ) ;
81+
82+ return result ;
3483} ;
35- // You can uncomment out this line to try your function
36- // console.log(getActiveClasses());
84+
85+
86+ console . log ( getActiveClasses ( ) ) ;
0 commit comments