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

Skip to content

Commit 112e98e

Browse files
committed
Prep exercises of week3 JavaScript
1 parent c717bd4 commit 112e98e

File tree

7 files changed

+166
-101
lines changed

7 files changed

+166
-101
lines changed

Week3/challenges/1-sum-entries.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,13 @@ Once you have found those numbers, multiply the numbers and store the result of
99

1010
const list = [1721, 979, 366, 299, 675, 1456];
1111
let result;
12-
13-
// Write your code here
1412

13+
list.forEach((num1, index1) => {
14+
const num2 = list.find((num, index2) => index2 > index1 && num1 + num === 2020);
15+
if (num2) {
16+
result = num1 * num2;
17+
}
18+
});
1519

1620
// TEST CODE, do not change
1721
console.assert(result === 514579, `The result is not correct, it is ${result}, but should be 514579`);

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,12 @@ 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
14-
15-
13+
list.forEach((num1, index1) => {
14+
list.forEach((num2, index2) => {
15+
const num3 = list.find((num, index3) => index3 !== index1 && index3 !== index2 && index2 !== index1 && num1 + num2 + num === 2020);
16+
if (num3) {
17+
result = num1 * num2 * num3;
18+
}})
19+
});
1620
// TEST CODE, do not change
1721
console.assert(result === 241861950, `The result is not correct, it is ${result}, but should be 241861950`);
Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,37 @@
1-
21
/**
32
* Credit to https://adventofcode.com/ for this exercise
4-
*
3+
*
54
* Each object in the passwordList gives a password policy and then the password.
65
* The times field says the minimal and maximal amount of times the letter should be in the password. So 1-3 means at least 1 time, at most 3 times.
76
* The letter field gives which letter should be counted
87
* The password field gives the password
9-
*
8+
*
109
* In the list 2 passwords are valid, the middle one is not as there is no b in the password.
11-
*
10+
*
1211
* We expect the output:
13-
*
12+
*
1413
* 'abcde' is VALID, a is present 1 times and should have been present at least 1 and at most 3 times
1514
* 'cdefg' is INVALID, b is present 0 times and should have been present at least 1 and at most 3 times
1615
* 'ccccccccc' is VALID, c is present 9 times and should have been present at least 2 and at most 9 times
1716
*/
1817

1918
const passwordList = [
20-
{ times: '1-3', letter: 'a', password: 'abcde'},
21-
{ times: '1-3', letter: 'b', password: 'cdefg'},
22-
{ times: '2-9', letter: 'c', password: 'ccccccccc'}
23-
];
19+
{ times: "1-3", letter: "a", password: "abcde" },
20+
{ times: "1-3", letter: "b", password: "cdefg" },
21+
{ times: "2-9", letter: "c", password: "ccccccccc" },
22+
];
23+
24+
passwordList.forEach((item) => {
25+
const { password, times, letter } = item;
26+
const timesArray = times.split("-");
27+
const passLen = Array.from(password).filter(character => character === letter).length;
28+
if (passLen >= timesArray[0] && passLen <= timesArray[1]) {
29+
console.log(
30+
`${password} is VALID, ${letter} is present ${passLen} times and should have been present at least ${timesArray[0]} and at most ${timesArray[1]} times`
31+
);
32+
} else {
33+
console.log(
34+
`${password} is INVALID, ${letter} is present ${passLen} times and should have been present at least ${timesArray[0]} and at most ${timesArray[1]} times`
35+
);
36+
}
37+
});

Week3/challenges/4-bank-account.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,27 @@ const bankAccount = {
2727
],
2828
};
2929

30+
const doTransaction = (amount, onSuccess, onFail, reason) => {
31+
const newAmount = bankAccount.currentBalance - amount;
32+
if (newAmount < 0) {
33+
onFail();
34+
} else {
35+
bankAccount.transactions.push({
36+
prevAmount: bankAccount.currentBalance,
37+
newAmount,
38+
reason,
39+
});
40+
bankAccount.currentBalance = newAmount;
41+
onSuccess();
42+
}
43+
};
44+
3045
const donateMoney = (amount, onSuccess, onFail) => {
31-
// TODO complete this function
46+
doTransaction(amount, onSuccess, onFail, "Donation");
3247
};
48+
3349
const payRent = (amount, onSuccess, onFail) => {
34-
// TODO complete this function
50+
doTransaction(amount, onSuccess, onFail, "Rent");
3551
};
3652

3753
/**

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@ import { modules, students, mentors, classes } from "./hyf.js";
88
* ['John', 'Mary']
99
*/
1010
const possibleMentorsForModule = (moduleName) => {
11-
// TODO complete this function
12-
};
13-
// You can uncomment out this line to try your function
14-
// console.log(possibleMentorsForModule('using-apis'));
11+
return mentors
12+
.filter(mentor => mentor.canTeach.includes(moduleName))
13+
.map(mentor => mentor.name)
14+
}
15+
console.log(possibleMentorsForModule('using-apis'));
16+
1517

1618
/**
1719
* Tjebbe wants to make it even easier for himself.
@@ -20,7 +22,7 @@ const possibleMentorsForModule = (moduleName) => {
2022
* It should return a single name.
2123
*/
2224
const findMentorForModule = (moduleName) => {
23-
// TODO complete this function
25+
return possibleMentorsForModule(moduleName)[Math.floor(Math.random()*possibleMentorsForModule(moduleName).length)]
2426
};
25-
// You can uncomment out this line to try your function
26-
// console.log(findMentorForModule('javascript'));
27+
28+
console.log(findMentorForModule('javascript'));

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

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,50 @@ 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 findCurrentModule = classes
16+
.filter(classN => classN.name.includes(className))
17+
.map(classN => classN.currentModule);
18+
19+
const findTeachers = mentors
20+
.filter(mentor => mentor.nowTeaching == findCurrentModule)
21+
.map(({name}) => ({name, role: 'mentor' }));
22+
23+
const findStudents = students
24+
.filter(student => student.class == className)
25+
.map(({name}) => ({name, role: 'student' }));
26+
27+
return [...findTeachers, ...findStudents];
1628
};
29+
1730
// You can uncomment out this line to try your function
18-
// console.log(getPeopleOfClass('class34'));
31+
console.log(getPeopleOfClass('class34'));
1932

2033
/**
2134
* We would like to have a complete overview of the current active classes.
2235
* First find the active classes, then for each get the people of that class.
2336
*
2437
* Should return an object with the class names as properties.
25-
* Each class name property contains an array identical to the return from `getPeopleFromClass`. So something like:
38+
* Each class name property contains an array identical to the return from getPeopleOfClass. So something like:
2639
*
2740
* {
2841
* class34: [{ name: 'John', role: 'student' }, { name: 'Mary', role: 'mentor' }],
2942
* class35: [{ name: 'Jane', role: 'student' }, { name: 'Steve', role: 'mentor' }]
3043
* }
3144
*/
3245
const getActiveClasses = () => {
33-
// TODO complete this function
46+
47+
const activeClasses = classes
48+
.filter(activeClass => activeClass.active == true);
49+
50+
const result = {};
51+
activeClasses.map(activeClass => {
52+
result[activeClass.name] = getPeopleOfClass(activeClass.name);
53+
});
54+
55+
return result;
56+
3457
};
3558
// You can uncomment out this line to try your function
36-
// console.log(getActiveClasses());
59+
console.log(getActiveClasses());
60+
61+
Lines changed: 73 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,84 @@
11
export const modules = [
2-
{ name: "html-css", displayName: "HTML/CSS" },
3-
{ name: "javascript", displayName: "JavaScript" },
4-
{ name: "browsers", displayName: "Browsers" },
5-
{ name: "using-apis", displayName: "Using APIs" },
6-
{ name: "node", displayName: "Node.js" },
7-
{ name: "databases", displayName: "Databases" },
8-
{ name: "react", displayName: "React" },
9-
{ name: "project", displayName: "Project" },
2+
{ name: "html-css", displayName: "HTML/CSS" },
3+
{ name: "javascript", displayName: "JavaScript" },
4+
{ name: "browsers", displayName: "Browsers" },
5+
{ name: "using-apis", displayName: "Using APIs" },
6+
{ name: "node", displayName: "Node.js" },
7+
{ name: "databases", displayName: "Databases" },
8+
{ name: "react", displayName: "React" },
9+
{ name: "project", displayName: "Project" },
1010
];
1111

1212
export const classes = [
13-
{
14-
name: "class32",
15-
startDate: "23-3-2021",
16-
active: false,
17-
graduationDate: "7-11-2021",
18-
},
19-
{
20-
name: "class33",
21-
startDate: "28-5-2021",
22-
active: false,
23-
graduationDate: "7-11-2021",
24-
},
25-
{
26-
name: "class34",
27-
startDate: "2-9-2021",
28-
active: true,
29-
currentModule: "react",
30-
},
31-
{
32-
name: "class35",
33-
startDate: "14-11-2021",
34-
active: true,
35-
currentModule: "using-apis",
36-
},
37-
{
38-
name: "class36",
39-
startDate: "5-1-2022",
40-
active: true,
41-
currentModule: "javascript",
42-
},
13+
{
14+
name: "class32",
15+
startDate: "23-3-2021",
16+
active: false,
17+
graduationDate: "7-11-2021",
18+
},
19+
{
20+
name: "class33",
21+
startDate: "28-5-2021",
22+
active: false,
23+
graduationDate: "7-11-2021",
24+
},
25+
{
26+
name: "class34",
27+
startDate: "2-9-2021",
28+
active: true,
29+
currentModule: "react",
30+
},
31+
{
32+
name: "class35",
33+
startDate: "14-11-2021",
34+
active: true,
35+
currentModule: "using-apis",
36+
},
37+
{
38+
name: "class36",
39+
startDate: "5-1-2022",
40+
active: true,
41+
currentModule: "javascript",
42+
},
4343
];
4444

4545
export const students = [
46-
{ name: "Fede", class: "class33", gitHubName: "fedefu", graduated: false },
47-
{ name: "Tjebbe", class: "class32", gitHubName: "Tjebbee", graduated: true },
48-
{ name: "Rob", class: "class34", gitHubName: "robvk", graduated: false },
49-
{
50-
name: "Wouter",
51-
class: "class35",
52-
gitHubName: "wouterkleijn",
53-
graduated: false,
54-
},
46+
{ name: "Fede", class: "class33", gitHubName: "fedefu", graduated: false },
47+
{ name: "Tjebbe", class: "class32", gitHubName: "Tjebbee", graduated: true },
48+
{ name: "Rob", class: "class34", gitHubName: "robvk", graduated: false },
49+
{
50+
name: "Wouter",
51+
class: "class35",
52+
gitHubName: "wouterkleijn",
53+
graduated: false,
54+
},
5555
];
5656

5757
export const mentors = [
58-
{
59-
name: "Stas",
60-
canTeach: ["javascript", "browsers", "using-apis"],
61-
nowTeaching: "javascript",
62-
},
63-
{
64-
name: "Andrej",
65-
canTeach: ["using-apis", "node"],
66-
},
67-
{
68-
name: "Shriyans",
69-
canTeach: ["react"],
70-
nowTeaching: "react",
71-
},
72-
{
73-
name: "Yash",
74-
canTeach: ["javascript", "using-apis"],
75-
},
76-
{
77-
name: "Rohan",
78-
canTeach: ["html/css/git", "javascript", "node"],
79-
},
80-
{
81-
name: "Collin",
82-
canTeach: ["browsers", "using-apis", "node"],
83-
},
58+
{
59+
name: "Stas",
60+
canTeach: ["javascript", "browsers", "using-apis"],
61+
nowTeaching: "javascript",
62+
},
63+
{
64+
name: "Andrej",
65+
canTeach: ["using-apis", "node"],
66+
},
67+
{
68+
name: "Shriyans",
69+
canTeach: ["react"],
70+
nowTeaching: "react",
71+
},
72+
{
73+
name: "Yash",
74+
canTeach: ["javascript", "using-apis"],
75+
},
76+
{
77+
name: "Rohan",
78+
canTeach: ["html/css/git", "javascript", "node"],
79+
},
80+
{
81+
name: "Collin",
82+
canTeach: ["browsers", "using-apis", "node"],
83+
},
8484
];

0 commit comments

Comments
 (0)