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

Skip to content

Commit f49bb1a

Browse files
committed
Solved the prep-exercises for week3
1 parent 622595b commit f49bb1a

File tree

2 files changed

+42
-8
lines changed

2 files changed

+42
-8
lines changed

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

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,14 @@ import { modules, students, mentors, classes } from "./hyf.js";
88
* ['John', 'Mary']
99
*/
1010
const possibleMentorsForModule = (moduleName) => {
11-
// TODO complete this function
11+
const mentorsCanTeachModule = mentors
12+
.filter(mentor => mentor.canTeach.includes(moduleName)) // Filtering the mentors that can teach given module
13+
.map(mentor => mentor.name); // Creating a new array
14+
15+
return mentorsCanTeachModule;
1216
};
1317
// You can uncomment out this line to try your function
14-
// console.log(possibleMentorsForModule('using-apis'));
18+
console.log(possibleMentorsForModule('using-apis'));
1519

1620
/**
1721
* Tjebbe wants to make it even easier for himself.
@@ -20,7 +24,16 @@ const possibleMentorsForModule = (moduleName) => {
2024
* It should return a single name.
2125
*/
2226
const findMentorForModule = (moduleName) => {
23-
// TODO complete this function
27+
const mentorsCanTeachModule = possibleMentorsForModule(moduleName);
28+
29+
if (!mentorsCanTeachModule.length) { // If the given module don't have any mentors matched
30+
return `No mentors found for ${moduleName}`;
31+
}
32+
33+
const randomIndex = Math.floor(Math.random() * mentorsCanTeachModule.length); // Getting a random number as index
34+
const randomMentor = mentorsCanTeachModule[randomIndex]; // Getting the random mentor
35+
36+
return randomMentor;
2437
};
2538
// You can uncomment out this line to try your function
26-
// console.log(findMentorForModule('javascript'));
39+
console.log(findMentorForModule('javascript'));

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

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,24 @@ 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+
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
1630
};
1731
// You can uncomment out this line to try your function
18-
// console.log(getPeopleOfClass('class34'));
32+
console.log(getPeopleOfClass('class36'));
1933

2034
/**
2135
* We would like to have a complete overview of the current active classes.
@@ -30,7 +44,14 @@ const getPeopleOfClass = (className) => {
3044
* }
3145
*/
3246
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;
3455
};
3556
// You can uncomment out this line to try your function
36-
// console.log(getActiveClasses());
57+
console.log(getActiveClasses());

0 commit comments

Comments
 (0)