@@ -7,11 +7,29 @@ import { modules, students, mentors, classes } from "./hyf.js";
7
7
* It should return an array of names. So something like:
8
8
* ['John', 'Mary']
9
9
*/
10
+
11
+ // I tried it first with forEach function and thought ahh I could use reduce
12
+ // const possibleMentorsForModule = (moduleName) => {
13
+ // let mentorForModule = [];
14
+ // mentors.forEach(mentor => {
15
+ // if (mentor.canTeach.includes(moduleName)) {
16
+ // mentorForModule.push(mentor.name);
17
+ // }
18
+ // });
19
+ // return mentorForModule;
20
+ // };
21
+
10
22
const possibleMentorsForModule = ( moduleName ) => {
11
- // TODO complete this function
23
+ return mentors . reduce ( ( acc , mentor ) => {
24
+ if ( mentor . canTeach . includes ( moduleName ) ) {
25
+ acc . push ( mentor . name ) ;
26
+ }
27
+ return acc ;
28
+ } , [ ] ) ;
29
+
12
30
} ;
13
31
// You can uncomment out this line to try your function
14
- // console.log(possibleMentorsForModule(' using-apis' ));
32
+ // console.log(possibleMentorsForModule(" using-apis" ));
15
33
16
34
/**
17
35
* Tjebbe wants to make it even easier for himself.
@@ -20,7 +38,14 @@ const possibleMentorsForModule = (moduleName) => {
20
38
* It should return a single name.
21
39
*/
22
40
const findMentorForModule = ( moduleName ) => {
23
- // TODO complete this function
41
+ const result = possibleMentorsForModule ( moduleName ) ;
42
+ if ( ! result . length ) return `No mentor found for module ${ moduleName } ` ;
43
+
44
+ if ( result . length === 1 ) {
45
+ return result [ 0 ] ;
46
+ }
47
+ const randomNum = Math . floor ( Math . random ( ) * result . length )
48
+ return result [ randomNum ] ;
24
49
} ;
25
50
// You can uncomment out this line to try your function
26
51
// console.log(findMentorForModule('javascript'));
0 commit comments