Skip to content

Commit 42621a1

Browse files
committed
finished prep-exercises week3
1 parent d299dd8 commit 42621a1

4 files changed

Lines changed: 3679 additions & 10 deletions

File tree

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

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,32 @@ import { modules, students, mentors, classes } from "./hyf.js";
88
* ['John', 'Mary']
99
*/
1010
const possibleMentorsForModule = (moduleName) => {
11-
// TODO complete this function
11+
const possibleMentors = []; // declared empty array to store filtered mentors
12+
13+
for(const {name, canTeach} of mentors) { // looping through each objects in mentors array using destructuring to extract the name and canTeach properties
14+
const filteredModules = canTeach.filter(word => word === moduleName); // filtering through canTeach array of each mentor if they teach the moduleName
15+
16+
if(filteredModules.length > 0) { // filteredModules can return empty array so I check if its length is greater than 0
17+
possibleMentors.push(name);
18+
}
19+
}
20+
return possibleMentors;
1221
};
22+
1323
// You can uncomment out this line to try your function
14-
// console.log(possibleMentorsForModule('using-apis'));
24+
console.log(possibleMentorsForModule('using-apis'));
1525

16-
/**
26+
/******
1727
* Tjebbe wants to make it even easier for himself.
1828
* Fill in this function that chooses a random mentor to teach the given module.
1929
*
2030
* It should return a single name.
2131
*/
2232
const findMentorForModule = (moduleName) => {
23-
// TODO complete this function
33+
const results = possibleMentorsForModule(moduleName); // will contain the names of mentors who can teach the module
34+
const randomMentor = Math.floor(Math.random() * results.length);
35+
return results[randomMentor];
2436
};
37+
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: 36 additions & 4 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+
const activeStudentsAndMentors = []; // will store students/mentors that are actively participating in className
17+
let activeModule; // to store the current module of the specified class
18+
19+
for(const {name, class: activeClassName} of students){ // loops through students' properties to get their name and class name
20+
if(activeClassName === className){ // checks if className of students equals activeClassName
21+
activeStudentsAndMentors.push({name, role: "student"}) // pushing name/role of student to active... (empty) array
22+
}
23+
}
24+
25+
for(const {name, currentModule} of classes){ // checks each name & currentModule from classes array
26+
if(name === className){ // if name (from each class object) is the same as className then execute the following:
27+
activeModule = currentModule; // value of currentModule ("react") will be stored in activeModule, will be use later
28+
}
29+
}
30+
31+
for(const {name, nowTeaching} of mentors){
32+
if(activeModule === nowTeaching){ // checks if activeModule is same as property nowTeaching from its respective mentor
33+
activeStudentsAndMentors.push({name, role: "mentor"}); // pushes the name of that mentor + their role to the active... array
34+
}
35+
}
36+
37+
return activeStudentsAndMentors;
1638
};
39+
1740
// 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,16 @@ const getPeopleOfClass = (className) => {
3053
* }
3154
*/
3255
const getActiveClasses = () => {
33-
// TODO complete this function
56+
let activeClassesObj = {}; // will store the name of each active class
57+
const activeClasses = classes.filter(c => c.active); // stores classes that have active: true from classes array
58+
59+
activeClasses.forEach(activeClass => {
60+
const peopleByClass = getPeopleOfClass(activeClass.name); // used activeClass.name from each class object and get results using prev. function
61+
62+
return activeClassesObj[activeClass.name] = peopleByClass; // set each object inside the activeClassesObj to the name of each activeClass array and store the result of using the prev. function
63+
});
64+
65+
return activeClassesObj;
3466
};
3567
// You can uncomment out this line to try your function
36-
// console.log(getActiveClasses());
68+
console.log(getActiveClasses());

0 commit comments

Comments
 (0)