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

Skip to content

Commit 76bdeb8

Browse files
committed
Javascript Third week prep-exercises solutions
1 parent f319fb8 commit 76bdeb8

File tree

2 files changed

+42
-9
lines changed

2 files changed

+42
-9
lines changed

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

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,38 @@ import { modules, students, mentors, classes } from "./hyf.js";
88
* ['John', 'Mary']
99
*/
1010
const possibleMentorsForModule = (moduleName) => {
11-
// TODO complete this function
11+
const mentorsForModule = mentors.filter(mentor =>
12+
mentor.canTeach.includes(moduleName)
13+
);
14+
return mentorsForModule.map(mentor => mentor.name);
1215
};
16+
17+
/*
18+
*** A different Solution Method ***
19+
20+
const possibleMentorsForModule = (moduleName) => {
21+
return mentors.flatMap(mentor =>
22+
mentor.canTeach.includes(moduleName) ? mentor.name : []
23+
);
24+
};
25+
*/
26+
1327
// You can uncomment out this line to try your function
14-
// console.log(possibleMentorsForModule('using-apis'));
28+
console.log(possibleMentorsForModule('using-apis'));
1529

1630
/**
1731
* Tjebbe wants to make it even easier for himself.
1832
* Fill in this function that chooses a random mentor to teach the given module.
1933
*
2034
* It should return a single name.
2135
*/
36+
2237
const findMentorForModule = (moduleName) => {
23-
// TODO complete this function
38+
const mentorsForModule = mentors.filter(mentor =>
39+
mentor.canTeach.includes(moduleName)
40+
);
41+
const randomIndex = Math.floor(Math.random() * mentorsForModule.length);
42+
return mentorsForModule[randomIndex].name;
2443
};
2544
// You can uncomment out this line to try your function
26-
// console.log(findMentorForModule('javascript'));
45+
console.log(findMentorForModule('javascript'));

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

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,19 @@ import { modules, students, mentors, classes } from "./hyf.js";
1111
*
1212
* [{ name: 'John', role: 'student' }, { name: 'Mary', role: 'mentor' }]
1313
*/
14-
const getPeopleOfClass = (className) => {
15-
// TODO complete this function
14+
const getPeopleOfClass = className => {
15+
const classInfo = classes.find(x => x.name === className);
16+
if (!classInfo) return [];
17+
18+
const studentsOfClass = students.filter(student => student.class === className).map(student => ({ name: student.name, role: "student" }));
19+
20+
const mentorsOfClass = mentors.filter(mentor => mentor.nowTeaching === classInfo.currentModule).map(mentor => ({ name: mentor.name, role: "mentor" }));
21+
22+
return [...studentsOfClass, ...mentorsOfClass];
1623
};
24+
1725
// You can uncomment out this line to try your function
18-
// console.log(getPeopleOfClass('class34'));
26+
console.log(getPeopleOfClass("class34"));
1927

2028
/**
2129
* We would like to have a complete overview of the current active classes.
@@ -30,7 +38,13 @@ const getPeopleOfClass = (className) => {
3038
* }
3139
*/
3240
const getActiveClasses = () => {
33-
// TODO complete this function
41+
return classes
42+
.filter(x => x.active)
43+
.reduce((activeClasses, currentClass) => {
44+
activeClasses[currentClass.name] = getPeopleOfClass(currentClass.name);
45+
return activeClasses;
46+
}, {});
3447
};
48+
3549
// You can uncomment out this line to try your function
36-
// console.log(getActiveClasses());
50+
console.log(getActiveClasses());

0 commit comments

Comments
 (0)