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

Skip to content

Commit e17f028

Browse files
committed
Added first versions of prep exercises
1 parent d22d305 commit e17f028

File tree

12 files changed

+325
-0
lines changed

12 files changed

+325
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Prep exercise - Objects and Arrays
2+
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:
4+
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?
7+
8+
Every real world scenario has its own `.js` file in this folder, go through them all.
9+
10+
## Things to think about
11+
12+
Next to thinking about each representation, also have a think about the following:
13+
14+
- 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: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"use strict";
2+
/**
3+
* Here we have an array for each of our 'entities' (students, teachers) and they share a property `class` that defines what class they are in.
4+
*/
5+
const school1 = {
6+
students: [
7+
{ name: "Rob", class: 1 },
8+
{ name: "Fede", class: 2 },
9+
],
10+
teachers: [{ name: "Stas", class: 1 }],
11+
};
12+
13+
/**
14+
* In this representation we have the classes property that has an array per class of everyone in that class.
15+
* We do not need a class property as we have the classes array, but as we do not specify a students or teachers array we have to add a `role` property
16+
*/
17+
const school2 = {
18+
classes: [
19+
[
20+
{ name: "Rob", role: "student" },
21+
{ name: "Stas", role: "teacher" },
22+
],
23+
[{ name: "Fede", role: "student" }],
24+
],
25+
};
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
"use strict";
2+
/**
3+
* Our state variable says what the traffic light's color is at that moment
4+
*/
5+
const simpleTrafficLight = {
6+
state: "red",
7+
};
8+
const currentTrafficLightState1 = simpleTrafficLight.state;
9+
10+
/**
11+
* The possibleStates define the options, then the currentStateIndex indicates where the traffic light is at that moment.
12+
*/
13+
const trafficLightWithStates = {
14+
possibleStates: ["green", "orange", "red"],
15+
currentStateIndex: 0,
16+
};
17+
const currentTrafficLightState2 =
18+
trafficLightWithStates.possibleStates[
19+
trafficLightWithStates.currentStateIndex
20+
];
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Prep exercise - traffic light
2+
3+
Let's have a deeper look at the working of traffic lights this week so that we can practice logic and loops. In the previous week we went into some different ways of representing a traffic light, now let's make the traffic light work. In `traffic-light-1.js` and `traffic-light-2.js` you will find the same requirements but with different ways of representing the traffic light. Have a look through the files and solve them.
4+
5+
## Things to think about
6+
7+
- Which way of representing the traffic light did you find better? Why?
8+
- What happens if you change the loop to a `do-while` loop instead of a `while` loop? Why?
9+
- We could have also used a `for` loop to make the traffic light do 2 full rotations. Do you think that would be better? Why or why not?
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
"use strict";
2+
/**
3+
* Our state variable says what the traffic light's color is at that moment
4+
*/
5+
const trafficLight = {
6+
state: "green",
7+
};
8+
9+
let rotations = 0;
10+
while (rotations < 2) {
11+
const currentTrafficLightState = trafficLight.state;
12+
console.log("The traffic light is on", currentTrafficLightState);
13+
14+
// TODO
15+
// if the color is green, turn it orange
16+
// if the color is orange, turn it red
17+
// if the color is red, add 1 to rotations and turn it green
18+
}
19+
20+
/**
21+
* The output should be:
22+
23+
The traffic light is on green
24+
The traffic light is on orange
25+
The traffic light is on red
26+
The traffic light is on green
27+
The traffic light is on orange
28+
The traffic light is on red
29+
30+
*/
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
"use strict";
2+
/**
3+
* Our state variable says what the traffic light's color is at that moment
4+
*/
5+
const trafficLight = {
6+
possibleStates: ["green", "orange", "red"],
7+
currentStateIndex: 0,
8+
};
9+
10+
let rotations = 0;
11+
while (rotations < 2) {
12+
const currentTrafficLightState =
13+
trafficLight.possibleStates[trafficLight.currentStateIndex];
14+
console.log("The traffic light is on", currentTrafficLightState);
15+
16+
// TODO
17+
// if the color is green, turn it orange
18+
// if the color is orange, turn it red
19+
// if the color is red, add 1 to rotations and turn it green
20+
}
21+
22+
/**
23+
* The output should be:
24+
25+
The traffic light is on green
26+
The traffic light is on orange
27+
The traffic light is on red
28+
The traffic light is on green
29+
The traffic light is on orange
30+
The traffic light is on red
31+
32+
*/
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Prep exercise - Traffic light
2+
3+
In the previous week we continued with our traffic light. Now that we also know what a function is we have one last look at the workings of a traffic light in a different way. Take a look at the `traffic-light.js` file and implement the same requirements as last week again, but then with another different way of organising.
4+
5+
## Things to think about
6+
7+
- This time the loop was changed to a for loop that will run the code 6 times. Why was that needed?
8+
- Why was the trafficLight added to the `main` function and not left at the top of the file?
9+
- What do you think is the advantage of having the `getCurrentTrafficLightState` and `getNextStateIndex` functions?
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
"use strict";
2+
/**
3+
* Our state variable says what the traffic light's color is at that moment
4+
*/
5+
6+
function getCurrentTrafficLightState(trafficLight) {
7+
// TODO
8+
// Should return `green`, `orange` or `red` depending on the state of the given parameter
9+
}
10+
11+
function getNextStateIndex(trafficLight) {
12+
// TODO
13+
// if the color is green, turn it orange
14+
// if the color is orange, turn it red
15+
// if the color is red, turn it green
16+
}
17+
18+
function main() {
19+
const trafficLight = {
20+
possibleStates: ["green", "orange", "red"],
21+
currentStateIndex: 0,
22+
};
23+
24+
for (let i = 0; i < 6; i++) {
25+
const currentTrafficLightState = getCurrentTrafficLightState(trafficLight);
26+
console.log("The traffic light is on", currentTrafficLightState);
27+
28+
trafficLight.currentStateIndex = getNextStateIndex(trafficLight);
29+
}
30+
}
31+
32+
main();
33+
/**
34+
* The output should be:
35+
36+
The traffic light is on green
37+
The traffic light is on orange
38+
The traffic light is on red
39+
The traffic light is on green
40+
The traffic light is on orange
41+
The traffic light is on red
42+
43+
*/
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Prep exercises - Dice experimentation
2+
3+
Let's do some experiments! One thing computers are great at is doing the same thing over and over and over again, so let's use that to see how random the random function in JavaScript is. In the `index.js` there is an explanation of what to implement. In essence we want to simulate the rolling of a die a lot of times and then track how many times a certain value was rolled.
4+
5+
## Things to think about
6+
7+
- The `valueCounts` is implemented as an array. Do you think there is another way to store this? Why do you think the decision was made to go with an array?
8+
- What do you think about the code division? Would you add another function or maybe remove one? Why?
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
"use strict";
2+
3+
function runExperiment(sampleSize) {
4+
const valueCounts = [0, 0, 0, 0, 0, 0];
5+
6+
// TODO
7+
// Write a for loop that iterates `sampleSize` times (sampleSize is a number).
8+
// In each loop iteration:
9+
//
10+
// 1. Generate a random integer between 1 and 6 (as if throwing a six-sided die).
11+
// 2. Add `1` to the element of the `valueCount` that corresponds to the random
12+
// value from the previous step. Use the first element of `valueCounts`
13+
// for keeping a count how many times the value 1 is thrown, the second
14+
// element for value 2, etc.
15+
16+
const results = [];
17+
18+
// TODO
19+
// Write a for..of loop for the `valueCounts` array created in the previous
20+
// loop. In each loop iteration:
21+
// 1. For each possible value of the die (1-6), compute the percentage of how
22+
// many times that value was thrown. Remember that the first value of
23+
// `valueCounts` represent the die value of 1, etc.
24+
// 2. Convert the computed percentage to a number string with a precision of
25+
// two decimals, e.g. '14.60'.
26+
// 3. Then push that string onto the `results` array.
27+
28+
return results;
29+
}
30+
31+
function main() {
32+
const sampleSizes = [100, 1000, 1000000];
33+
34+
// TODO
35+
// Write a for..of loop that calls the `runExperiment()` function for each
36+
// value of the `sampleSizes` array.
37+
// Log the results of each experiment as well as the experiment size to the
38+
// console.
39+
// The expected output could look like this:
40+
//
41+
// [ '26.00', '17.00', '10.00', '19.00', '16.00', '12.00' ] 100
42+
// [ '14.60', '17.10', '19.30', '15.50', '16.70', '16.80' ] 1000
43+
// [ '16.71', '16.68', '16.69', '16.66', '16.67', '16.59' ] 1000000
44+
}
45+
46+
main();
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Prep exercise - banking
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 a very difficult exercise with a lot of moving parts. 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+
- Why do you think the `onSuccess` and `onFail` callback functions were used? What is the advantage of that?
8+
- Did you create extra functions? Why or why not?
9+
- How would you test this code?
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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+
const donateMoney = (amount, onSuccess, onFail) => {};
30+
const payRent = (amount, onSuccess, onFail) => {};
31+
32+
/**
33+
* TEST CODE. DO NOT EDIT
34+
*/
35+
36+
const onSuccess = () => {
37+
console.log("Payment successful! Thank you!");
38+
};
39+
const onFail = () => {
40+
console.log("You do not have enough money to make this payment.");
41+
};
42+
donateMoney(100, onSuccess, onFail);
43+
console.log(bankAccount);
44+
payRent(100, onSuccess, onFail);
45+
console.log(bankAccount);
46+
donateMoney(100, onSuccess, onFail);
47+
console.log(bankAccount);
48+
49+
/**
50+
* The console should print out the following:
51+
52+
Payment successful! Thank you!
53+
{
54+
currentBalance: 150,
55+
transactions: [
56+
{ prevAmount: 350, newAmount: 250, reason: 'Donation' },
57+
{ prevAmount: 250, newAmount: 150, reason: 'Donation' }
58+
]
59+
}
60+
Payment successful! Thank you!
61+
{
62+
currentBalance: 50,
63+
transactions: [
64+
{ prevAmount: 350, newAmount: 250, reason: 'Donation' },
65+
{ prevAmount: 250, newAmount: 150, reason: 'Donation' },
66+
{ prevAmount: 150, newAmount: 50, reason: 'Rent' }
67+
]
68+
}
69+
You do not have enough money to make this payment.
70+
{
71+
currentBalance: 50,
72+
transactions: [
73+
{ prevAmount: 350, newAmount: 250, reason: 'Donation' },
74+
{ prevAmount: 250, newAmount: 150, reason: 'Donation' },
75+
{ prevAmount: 150, newAmount: 50, reason: 'Rent' }
76+
]
77+
}
78+
79+
*
80+
*/

0 commit comments

Comments
 (0)