Skip to content

Commit 2ebcee4

Browse files
committed
prep ex is completed
1 parent 051948c commit 2ebcee4

2 files changed

Lines changed: 78 additions & 10 deletions

File tree

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

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,16 @@ import { modules, students, mentors, classes } from "./hyf.js";
88
* ['John', 'Mary']
99
*/
1010
const possibleMentorsForModule = (moduleName) => {
11-
// TODO complete this function
11+
let mentorList = [];
12+
13+
mentorList = mentors.filter((mentor) => mentor.canTeach.includes(moduleName));
14+
15+
const mentorsNames = mentorList.map((mentor) => mentor.name);
16+
return mentorsNames;
1217
};
18+
1319
// You can uncomment out this line to try your function
14-
// console.log(possibleMentorsForModule('using-apis'));
20+
console.log(possibleMentorsForModule('using-apis'));
1521

1622
/**
1723
* Tjebbe wants to make it even easier for himself.
@@ -20,7 +26,19 @@ const possibleMentorsForModule = (moduleName) => {
2026
* It should return a single name.
2127
*/
2228
const findMentorForModule = (moduleName) => {
23-
// TODO complete this function
29+
// Filter mentors who can teach the specified module
30+
const availableMentors = mentors.filter((mentor) => mentor.canTeach.includes(moduleName));
31+
32+
// Check if there are mentors available for the module
33+
if (availableMentors.length === 0) {
34+
return 'No available mentors for this module';
35+
}
36+
37+
// Choose a random mentor from the available ones
38+
const randomMentor = availableMentors[Math.floor(Math.random() * availableMentors.length)];
39+
40+
// Return the name of the chosen mentor
41+
return randomMentor.name;
2442
};
2543
// You can uncomment out this line to try your function
26-
// console.log(findMentorForModule('javascript'));
44+
console.log(findMentorForModule('javascript'));

Week3/prep-exercises/1-hyf-program/2-class-list.js

Lines changed: 56 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,33 @@ import { modules, students, mentors, classes } from "./hyf.js";
1212
* [{ name: 'John', role: 'student' }, { name: 'Mary', role: 'mentor' }]
1313
*/
1414
const 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
*/
3255
const 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

Comments
 (0)