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

Skip to content

Commit dc9c46b

Browse files
committed
week3 completed
1 parent 88c2d3c commit dc9c46b

File tree

2 files changed

+60
-4
lines changed

2 files changed

+60
-4
lines changed

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

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ 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+
// then we take the name of the teacher and put it in a new array
12+
1013
const possibleMentorsForModule = (moduleName) => {
14+
// first we take all the teacher who can teach the 'using apis' module
1115
const mentorList = [];
1216
mentors.forEach((mentor) => {
1317
if (mentor.canTeach.includes(moduleName)) {
@@ -17,14 +21,20 @@ const possibleMentorsForModule = (moduleName) => {
1721
return mentorList;
1822
};
1923
// You can uncomment out this line to try your function
20-
console.log(`${possibleMentorsForModule('using-apis')} can teach API module`);
24+
// console.log(`${possibleMentorsForModule('using-apis')} can teach API module`);
2125

2226
/**
2327
* Tjebbe wants to make it even easier for himself.
2428
* Fill in this function that chooses a random mentor to teach the given module.
2529
*
2630
* It should return a single name.
2731
*/
32+
33+
// const maxNum = 10;
34+
// const minNum = 0;
35+
// const randomNumber = Math.floor(Math.random() * (maxNum - minNum + 1) + minNum);
36+
// console.log(randomNumber);
37+
2838
const findMentorForModule = (moduleName) => {
2939
const mentorList = [];
3040
let randomNum;
@@ -33,8 +43,11 @@ const findMentorForModule = (moduleName) => {
3343
mentorList.push(mentor.name);
3444
}
3545
});
36-
randomNum = Math.floor(Math.random() * (mentorList.length - 1 - 0 + 1)) + 0;
37-
return mentorList[randomNum];
46+
const maxNum = mentorList.length - 1;
47+
const minNum = 0;
48+
randomNum = Math.floor(Math.random() * (maxNum - minNum + 1) + minNum);
49+
const randomMentor = mentorList[randomNum];
50+
return randomMentor;
3851
};
3952
// You can uncomment out this line to try your function
4053
console.log(

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

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,49 @@ console.log(getPeopleOfClass('class34'));
4949
*/
5050
const getActiveClasses = () => {
5151
// TODO complete this function
52+
53+
const activeClasses = classes
54+
.map((eachClass) => {
55+
if (eachClass.active) return eachClass.name;
56+
})
57+
.filter((eachClass) => eachClass !== undefined);
58+
59+
const activeStudent = students
60+
.map((student) => {
61+
if (student.graduated == false)
62+
return { name: student.name, class: student.class, role: 'student' };
63+
})
64+
.filter((student) => student !== undefined);
65+
66+
const activeModule = classes
67+
.filter((eachClass) => eachClass.active)
68+
.map((eachClass) => eachClass.currentModule);
69+
70+
const activeMentor = mentors
71+
.map((mentor) => {
72+
if (activeModule.includes(mentor.nowTeaching)) {
73+
const mentorName = mentor.name;
74+
const className = classes
75+
.filter((eachClass) => eachClass.currentModule == mentor.nowTeaching)
76+
.map((eachClass) => eachClass.name)
77+
.pop();
78+
return { name: mentorName, class: className, role: 'mentor' };
79+
}
80+
})
81+
.filter((mentor) => mentor !== undefined);
82+
83+
const activeClassObj = activeClasses.map((eachClass) => {
84+
const studentList = activeStudent.filter(
85+
(student) => student.class == eachClass
86+
);
87+
const mentorList = activeMentor.filter(
88+
(mentor) => mentor.class == eachClass
89+
);
90+
return { [eachClass]: [...studentList, ...mentorList] };
91+
});
92+
93+
return JSON.stringify(activeClassObj, null, 2);
5294
};
5395
// You can uncomment out this line to try your function
54-
// console.log(getActiveClasses());
96+
console.log('active classes:');
97+
console.log(getActiveClasses());

0 commit comments

Comments
 (0)