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

Skip to content

Commit 501c4a9

Browse files
committed
Finalized week3 JS assignment
1 parent e4563ec commit 501c4a9

File tree

2 files changed

+36
-6
lines changed

2 files changed

+36
-6
lines changed

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { modules, students, mentors, classes } from "./hyf.js";
88
* ['John', 'Mary']
99
*/
1010

11-
// I tried it first with forEach function and thought ahh I could use reduce
11+
// I tried it first with forEach and thought ahh i can use reduce
1212
// const possibleMentorsForModule = (moduleName) => {
1313
// let mentorForModule = [];
1414
// mentors.forEach(mentor => {
@@ -19,7 +19,7 @@ import { modules, students, mentors, classes } from "./hyf.js";
1919
// return mentorForModule;
2020
// };
2121

22-
const possibleMentorsForModule = (moduleName) => {
22+
const possibleMentorsForModule = (moduleName) => {
2323
return mentors.reduce((acc, mentor) => {
2424
if (mentor.canTeach.includes(moduleName)) {
2525
acc.push(mentor.name);
@@ -28,6 +28,15 @@ const possibleMentorsForModule = (moduleName) => {
2828
}, []);
2929

3030
};
31+
32+
export const mentorForModule = (moduleName) => {
33+
return mentors.reduce((acc, mentor) => {
34+
if (mentor.nowTeaching === moduleName) {
35+
acc.push(mentor);
36+
}
37+
return acc;
38+
}, []);
39+
};
3140
// You can uncomment out this line to try your function
3241
// console.log(possibleMentorsForModule("using-apis"));
3342

@@ -39,7 +48,7 @@ const possibleMentorsForModule = (moduleName) => {
3948
*/
4049
const findMentorForModule = (moduleName) => {
4150
const result = possibleMentorsForModule(moduleName);
42-
if(!result.length) return `No mentor found for module ${moduleName}`;
51+
if( !result ) return `No mentor found for module ${moduleName}`;
4352

4453
if(result.length === 1){
4554
return result[0];

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

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { modules, students, mentors, classes } from "./hyf.js";
2-
2+
import { mentorForModule } from "./1-find-mentors.js";
33
/**
44
* We would like to have a list of everyone that is currently participating in a class.
55
* This means the students, but also the mentors that are currently teaching the class.
@@ -11,8 +11,22 @@ import { modules, students, mentors, classes } from "./hyf.js";
1111
*
1212
* [{ name: 'John', role: 'student' }, { name: 'Mary', role: 'mentor' }]
1313
*/
14+
15+
16+
const filterStudentsByClassName = className => {
17+
return students.filter(student => {
18+
return student.class === className && student.graduated === false})
19+
.map(({name}) => ({name, role: 'student'}));
20+
};
21+
1422
const getPeopleOfClass = (className) => {
15-
// TODO complete this function
23+
const participatingStudents = filterStudentsByClassName(className);
24+
25+
const specificClass = classes.find(({name}) => name === className);
26+
const participatingMentors = mentorForModule(specificClass.currentModule)
27+
.map(({name}) => ({name, role:'mentor'}));
28+
29+
return [...participatingStudents, ...participatingMentors];
1630
};
1731
// You can uncomment out this line to try your function
1832
// console.log(getPeopleOfClass('class34'));
@@ -30,7 +44,14 @@ const getPeopleOfClass = (className) => {
3044
* }
3145
*/
3246
const getActiveClasses = () => {
33-
// TODO complete this function
47+
const activeClasses = classes
48+
.filter(({active}) => { return active === true })
49+
.map(({name}) => ({name}));
50+
51+
return activeClasses.reduce((acc, activeClass) => {
52+
acc[activeClass.name] = getPeopleOfClass(activeClass.name);
53+
return acc;
54+
}, {});
3455
};
3556
// You can uncomment out this line to try your function
3657
// console.log(getActiveClasses());

0 commit comments

Comments
 (0)