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

Skip to content

Commit 4b600b4

Browse files
committed
rewritten week 4 exercise
1 parent db0a8ae commit 4b600b4

File tree

9 files changed

+297
-64
lines changed

9 files changed

+297
-64
lines changed
Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,24 @@
11
# Prep exercise - Objects and Arrays
22

3-
Objects and Arrays form the basis of pretty much all data structures in JavaScript and allow us to represent the state of the world to be manipulated. This exercise is all about how to represent objects in the real world in an 'IT' way. We will be offering different ways of storing regular objects and for each we would like you to think about the following questions:
3+
Objects and Arrays form the basis of pretty much all data structures in JavaScript and allow us to represent the state of the world to be manipulated. This exercise is all about how to represent objects in the real world in an 'IT' way. By thinking about the data structure you can make it easier to implement certain functionality which will help code stay simple and readable!
44

5-
- What are the advantages and disadvantages of having it represented in each way?
6-
- Do you think there is another way of representing the object? If so, what is it and what do you see as the advantage of using that way?
5+
## Traffic light
76

8-
Every real world scenario has its own `.js` file in this folder, go through them all.
7+
In the `traffic-light-1.js` and `traffic-light-2.js` files we give two different ways of representing a traffic light. Have a look at the files and think about the following:
8+
9+
- In the first version we create an object with a state property. Why do you think we do this? Why not just a variable?
10+
- In the second version we add extra information (the `possibleStates` property). What do you think the advantage is of that?
11+
- In the second version the `stateIndex` property is a number, why do you think that is?
12+
13+
## HackYourFuture program
14+
15+
In the `hyf.js` file we have a more complex representation of the hyf program. We have divided the hyf world into 4 what we call `entities`: `modules`, `classes`, `students`, `mentors`. The `export` statements are for week 4, you can ignore those for now! Have a look and think about the following:
16+
17+
- Why do you think we have a `name` and `displayName` property for the `modules`?
18+
- Do you think `active` and `start`/`graduationDate` are both needed in the `classes` array? Why or why not?
919

1020
## Things to think about
1121

12-
Next to thinking about each representation, also have a think about the following:
22+
Next to the questions specific to each representation, also have a think about the following:
1323

1424
- In all of the examples, you will see that objects and arrays are mostly defined using a `const` statement rather than a `let` even if we change the value of the object or array, why do you think this is the case?
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
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" },
10+
];
11+
12+
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+
},
43+
];
44+
45+
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+
},
55+
];
56+
57+
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+
},
84+
];

Week1/prep-exercises/1-objects-and-arrays/school.js

Lines changed: 0 additions & 50 deletions
This file was deleted.
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/**
2+
* It is time to write some bigger code! You have a bankAccount that is modeled as given.
3+
*
4+
* Finish the two functions that will donate money (donateMoney) and pay rent (payRent).
5+
* If you do not have enough funds, call the onFail function given and don't change the bankAccount.
6+
* If you do have the funds, update the bankAccount accordingly.
7+
*
8+
* TIP: have a look at the test code to get more information on what needs to happen
9+
* TIP: a lot of the things the functions do are the same, you may want to create one or more other functions to not duplicate code
10+
*/
11+
12+
const bankAccount = {
13+
// The currentBalance is how much money you have in your bankAccount.
14+
currentBalance: 250,
15+
// The transactions are a list of changes so that you can keep track.
16+
transactions: [
17+
/**
18+
* The prevAmount is what your balance was before the transaction,
19+
* the newAmount is what your balance was after the transaction
20+
* and the reason is what the transaction was about
21+
*/
22+
{
23+
prevAmount: 350,
24+
newAmount: 250,
25+
reason: "Donation",
26+
},
27+
],
28+
};
29+
30+
/**
31+
* The donateMoney and payRent do a lot of the same thing, so created a separate function
32+
*/
33+
const createPayment = (amount, onSuccess, onFail, reason) => {
34+
const newAmount = bankAccount.currentBalance - amount;
35+
36+
if (newAmount < 0) {
37+
onFail();
38+
39+
return;
40+
}
41+
42+
bankAccount.transactions.push({
43+
prevAmount: bankAccount.currentBalance,
44+
newAmount,
45+
reason,
46+
});
47+
bankAccount.currentBalance = newAmount;
48+
49+
onSuccess();
50+
};
51+
const donateMoney = (amount, onSuccess, onFail) => {
52+
createPayment(amount, onSuccess, onFail, "Donation");
53+
};
54+
const payRent = (amount, onSuccess, onFail) => {
55+
createPayment(amount, onSuccess, onFail, "Rent");
56+
};
57+
58+
/**
59+
* TEST CODE. DO NOT EDIT
60+
*/
61+
62+
const onSuccessEnglish = () => {
63+
console.log("Payment successful! Thank you!");
64+
};
65+
const onFailEnglish = () => {
66+
console.log("You do not have enough money to make this payment.");
67+
};
68+
69+
const onSuccessDutch = () => {
70+
console.log("Betaling geslaagd! Dank u!");
71+
};
72+
const onFailDutch = () => {
73+
console.log("U heeft niet voldoende saldo om deze betaling te doen.");
74+
};
75+
76+
donateMoney(100, onSuccessEnglish, onFailEnglish);
77+
console.log(bankAccount);
78+
79+
payRent(100, onSuccessEnglish, onFailEnglish);
80+
console.log(bankAccount);
81+
82+
donateMoney(100, onSuccessDutch, onFailDutch);
83+
console.log(bankAccount);
84+
85+
/**
86+
* The console should print out the following:
87+
88+
Payment successful! Thank you!
89+
{
90+
currentBalance: 150,
91+
transactions: [
92+
{ prevAmount: 350, newAmount: 250, reason: 'Donation' },
93+
{ prevAmount: 250, newAmount: 150, reason: 'Donation' }
94+
]
95+
}
96+
Payment successful! Thank you!
97+
{
98+
currentBalance: 50,
99+
transactions: [
100+
{ prevAmount: 350, newAmount: 250, reason: 'Donation' },
101+
{ prevAmount: 250, newAmount: 150, reason: 'Donation' },
102+
{ prevAmount: 150, newAmount: 50, reason: 'Rent' }
103+
]
104+
}
105+
U heeft niet voldoende saldo om deze betaling te doen.
106+
{
107+
currentBalance: 50,
108+
transactions: [
109+
{ prevAmount: 350, newAmount: 250, reason: 'Donation' },
110+
{ prevAmount: 250, newAmount: 150, reason: 'Donation' },
111+
{ prevAmount: 150, newAmount: 50, reason: 'Rent' }
112+
]
113+
}
114+
115+
*
116+
*/

Week4/prep-exercises/1-banking/README.md

Lines changed: 0 additions & 9 deletions
This file was deleted.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import {
2+
modules,
3+
students,
4+
mentors,
5+
classes,
6+
} from "../../../Week1/prep-exercises/1-objects-and-arrays/hyf";
7+
8+
/**
9+
* Tjebbe would like help to get a list of possible mentors for a module.
10+
* Fill in this function that finds all the mentors that can teach the given module.
11+
*
12+
* It should return an array of names. So something like:
13+
* ['John', 'Mary']
14+
*/
15+
const possibleMentorsForModule = (moduleName) => {
16+
// TODO complete this function
17+
};
18+
// You can uncomment out this line to try your function
19+
// console.log(possibleMentorsForModule('using-apis'));
20+
21+
/**
22+
* Tjebbe wants to make it even easier for himself.
23+
* Fill in this function that chooses a random mentor to teach the given module.
24+
*
25+
* It should return a single name.
26+
*/
27+
const findMentorForModule = (moduleName) => {
28+
// TODO complete this function
29+
};
30+
// You can uncomment out this line to try your function
31+
// console.log(findMentorForModule('javascript'));
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import {
2+
modules,
3+
students,
4+
mentors,
5+
classes,
6+
} from "../../../Week1/prep-exercises/1-objects-and-arrays/hyf";
7+
8+
/**
9+
* We would like to have a list of everyone that is currently participating in a class.
10+
* This means the students, but also the mentors that are currently teaching the class.
11+
* The students should be self explanatory, but to find the mentors you will need to follow these steps:
12+
* - Check what the `currentModule` of the class is
13+
* - Find the mentor(s) that are `nowTeaching` that module
14+
*
15+
* Should return the list of names and their roles. So something like:
16+
*
17+
* [{ name: 'John', role: 'student' }, { name: 'Mary', role: 'mentor' }]
18+
*/
19+
const getPeopleOfClass = (className) => {
20+
// TODO complete this function
21+
};
22+
// You can uncomment out this line to try your function
23+
// console.log(getPeopleOfClass('class34'));
24+
25+
/**
26+
* We would like to have a complete overview of the current active classes.
27+
* First find the active classes, then for each get the people of that class.
28+
*
29+
* Should return an object with the class names as properties.
30+
* Each class name property contains an array identical to the return from `getPeopleFromClass`. So something like:
31+
*
32+
* {
33+
* class34: [{ name: 'John', role: 'student' }, { name: 'Mary', role: 'mentor' }],
34+
* class35: [{ name: 'Jane', role: 'student' }, { name: 'Steve', role: 'mentor' }]
35+
* }
36+
*/
37+
const getActiveClasses = () => {
38+
// TODO complete this function
39+
};
40+
// You can uncomment out this line to try your function
41+
// console.log(getActiveClasses());
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Prep exercise - HackYourFuture program
2+
3+
This week is all about combining everything we know and learning how to solve problems. To help with that, we're going to do some exercises using the data structure of the HackYourFuture program we made in week 1. As it is a prep exercise, it is something you can do together to get to the solution! Have a look at the `index.js` file for the instructions.
4+
5+
## Things to think about
6+
7+
After completing the exercise, have a think about the following things:
8+
9+
- We split the functions into multiple files. Why do you think we do that?
10+
- Note how some functions can be used by others to solve its problem. A lot of times when we are stuck in programming it is because we are trying to solve everything at once. It then helps to split the problem up into steps!

0 commit comments

Comments
 (0)