@@ -12,10 +12,24 @@ import { modules, students, mentors, classes } from "./hyf.js";
12
12
* [{ name: 'John', role: 'student' }, { name: 'Mary', role: 'mentor' }]
13
13
*/
14
14
const getPeopleOfClass = ( className ) => {
15
- // TODO complete this function
15
+ const classInfo = classes . find ( cls => cls . name === className ) ; // Find the class that passed as an argument
16
+
17
+ if ( ! classInfo ) {
18
+ return `There is no ${ className } ` ;
19
+ }
20
+
21
+ const classStudents = students
22
+ . filter ( student => student . class === className ) // Filtering the students array that matches with className
23
+ . map ( student => ( { name : student . name , role : 'student' } ) ) ; // Returning new array with student name and role: 'student
24
+
25
+ const classMentors = mentors
26
+ . filter ( mentor => mentor . nowTeaching === classInfo . currentModule ) // Filtering the mentors array that matches with the current module of class
27
+ . map ( mentor => ( { name : mentor . name , role : 'mentor' } ) ) ; // Returning new array
28
+
29
+ return [ ...classStudents , ...classMentors ] ; // Destructuring the arrays that assign to values so we can combine them
16
30
} ;
17
31
// You can uncomment out this line to try your function
18
- // console.log(getPeopleOfClass('class34 '));
32
+ console . log ( getPeopleOfClass ( 'class36 ' ) ) ;
19
33
20
34
/**
21
35
* We would like to have a complete overview of the current active classes.
@@ -30,7 +44,14 @@ const getPeopleOfClass = (className) => {
30
44
* }
31
45
*/
32
46
const getActiveClasses = ( ) => {
33
- // TODO complete this function
47
+ const activeClasses = classes . filter ( cls => cls . active ) ; // Finding the active classes
48
+
49
+ const result = activeClasses . reduce ( ( acc , activeClass ) => { // Using reduce function get the each active classes
50
+ acc [ activeClass . name ] = getPeopleOfClass ( activeClass . name ) ; // Assign the each active class to the function we used upside to par classname as key
51
+ return acc ;
52
+ } , { } ) ; // Initialize the first value as empty object
53
+
54
+ return result ;
34
55
} ;
35
56
// You can uncomment out this line to try your function
36
- // console.log(getActiveClasses());
57
+ console . log ( getActiveClasses ( ) ) ;
0 commit comments