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

Skip to content

Commit aede616

Browse files
committed
Added solutions for week 3 exercises
1 parent c064546 commit aede616

File tree

6 files changed

+80
-9
lines changed

6 files changed

+80
-9
lines changed

Week3/challenges/1-sum-entries.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,14 @@ Once you have found those numbers, multiply the numbers and store the result of
1010
const list = [1721, 979, 366, 299, 675, 1456];
1111
let result;
1212

13-
// Write your code here
13+
for (let i =0; i , list.length; i++) {
14+
for ( let j = i + 1; j < list.length; j++ ) {
15+
if (list[i] + list[j] === 2020 ){
16+
result = list[i] * list[j];
17+
break;
18+
}
19+
}
20+
}
1421

1522

1623
// TEST CODE, do not change

Week3/challenges/2-sum-three-entries.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,16 @@ Once you have found those numbers, multiply the numbers and store the result of
1010
const list = [1721, 979, 366, 299, 675, 1456];
1111
let result;
1212

13-
// Write your code here
13+
for (let i =0; i < list.length; i++) {
14+
for ( let j = i + 1; j < list.length; j++ ) {
15+
for ( let k = j + 1; k < list.length; k++ ) {
16+
if (list[i] + list[j] + list[k] === 2020 ){
17+
result = list[i] * list[j] * list[k];
18+
break;
19+
}
20+
}
21+
}
22+
}
1423

1524

1625
// TEST CODE, do not change

Week3/challenges/3-password-validation.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,11 @@ const passwordList = [
2020
{ times: '1-3', letter: 'a', password: 'abcde'},
2121
{ times: '1-3', letter: 'b', password: 'cdefg'},
2222
{ times: '2-9', letter: 'c', password: 'ccccccccc'}
23-
];
23+
];
24+
passwordList.forEach(item => {
25+
const [ min, max] = item.times.split("-").map(Number);
26+
const leetterCount = item.password.split("").filter(char => char === item.letter).length;
27+
const isValid = leetterCount >= min && leetterCount <= max;
28+
29+
console.log(`Password '${item.password}' is ${isValid? "VALID" : "INVALID"}`);
30+
})

Week3/challenges/4-bank-account.js

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,32 @@ const bankAccount = {
2828
};
2929

3030
const donateMoney = (amount, onSuccess, onFail) => {
31-
// TODO complete this function
31+
if (bankAccount.currentBalance >= amount) {
32+
bankAccount.currentBalance -= amount;
33+
34+
bankAccount.transactions.push({
35+
prevAmount: bankAccount.currentBalance + amount,
36+
newAmount: bankAccount.currentBalance,
37+
reason: "Donation",
38+
});
39+
onSuccess();
40+
}else {
41+
onFail();
42+
}
3243
};
3344
const payRent = (amount, onSuccess, onFail) => {
34-
// TODO complete this function
45+
if (bankAccount.currentBalance >= amount) {
46+
bankAccount.currentBalance -= amount;
47+
48+
bankAccount.transactions.push({
49+
prevAmount: bankAccount.currentBalance + amount,
50+
newAmount: bankAccount.currentBalance,
51+
reason: "Rent",
52+
});
53+
onSuccess();
54+
} else {
55+
onFail();
56+
}
3557
};
3658

3759
/**

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ import { modules, students, mentors, classes } from "./hyf.js";
88
* ['John', 'Mary']
99
*/
1010
const possibleMentorsForModule = (moduleName) => {
11-
// TODO complete this function
11+
const mentorsTeachingModule = mentors.filter((mentor) => mentor.canTeach.includes(moduleName));
12+
13+
return mentorsTeachingModule.map((mentor) => mentor.name);
1214
};
1315
// You can uncomment out this line to try your function
1416
// console.log(possibleMentorsForModule('using-apis'));
@@ -20,7 +22,14 @@ const possibleMentorsForModule = (moduleName) => {
2022
* It should return a single name.
2123
*/
2224
const findMentorForModule = (moduleName) => {
23-
// TODO complete this function
25+
const possibleMentors = possibleMentorsForModule(moduleName);
26+
27+
if (!possibleMentors.length) {
28+
console.error(`No mentors found for module ${moduleName}`);
29+
return;
30+
}
31+
32+
return possibleMentors[Math.floor(Math.random() * possibleMentors.length)];
2433
};
2534
// You can uncomment out this line to try your function
2635
// console.log(findMentorForModule('javascript'));

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

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,18 @@ 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);
16+
if (!classInfo) {
17+
console.error(`Class ${className} not found.`);
18+
return [];
19+
}
20+
const studentsOfClass = students.filter(students => students.class === className)
21+
.map(students => ({ name: students.name, role:'student' }));
22+
23+
const mentorsOfClass = mentors.filter(mentors => mentors.nowTeaching === classInfo.currentModule)
24+
.map(mentors => ({ name: mentors.name, role:'mentor' }));
25+
26+
return [...studentsOfClass,...mentorsOfClass];
1627
};
1728
// You can uncomment out this line to try your function
1829
// console.log(getPeopleOfClass('class34'));
@@ -30,7 +41,13 @@ const getPeopleOfClass = (className) => {
3041
* }
3142
*/
3243
const getActiveClasses = () => {
33-
// TODO complete this function
44+
const activeClasses = classes.filter(cls => cls.active);
45+
46+
const result = {};
47+
activeClasses.forEach(activeClasses => {
48+
result[activeClasses.name] = getPeopleOfClass(activeClasses.name);
49+
});
50+
return result;
3451
};
3552
// You can uncomment out this line to try your function
3653
// console.log(getActiveClasses());

0 commit comments

Comments
 (0)