Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e4563ec

Browse files
committed
Completed the find mentors exercise.
1 parent 9b9ec58 commit e4563ec

File tree

1 file changed

+28
-3
lines changed

1 file changed

+28
-3
lines changed

Week3/prep-exercises/1-hyf-program/1-find-mentors.js

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,29 @@ import { modules, students, mentors, classes } from "./hyf.js";
77
* It should return an array of names. So something like:
88
* ['John', 'Mary']
99
*/
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+
1022
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+
1230
};
1331
// You can uncomment out this line to try your function
14-
// console.log(possibleMentorsForModule('using-apis'));
32+
// console.log(possibleMentorsForModule("using-apis"));
1533

1634
/**
1735
* Tjebbe wants to make it even easier for himself.
@@ -20,7 +38,14 @@ const possibleMentorsForModule = (moduleName) => {
2038
* It should return a single name.
2139
*/
2240
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];
2449
};
2550
// You can uncomment out this line to try your function
2651
// console.log(findMentorForModule('javascript'));

0 commit comments

Comments
 (0)