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

Skip to content

Commit d9e4268

Browse files
committed
Suggested prep exercise updates
1 parent 2da27df commit d9e4268

File tree

8 files changed

+150
-89
lines changed

8 files changed

+150
-89
lines changed
Lines changed: 49 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,50 @@
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-
};
1+
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+
];
1211

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-
};
12+
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: true,
23+
currentModule: 'project',
24+
},
25+
{
26+
name: 'class34',
27+
startDate: '2-9-2021',
28+
active: true,
29+
currentModule: 'node',
30+
},
31+
{
32+
name: 'class35',
33+
startDate: '14-11-2021',
34+
active: true,
35+
currentModule: 'javascript',
36+
},
37+
];
38+
39+
const students = [
40+
{ name: 'Fede', class: 'class34', gitHubName: 'fedefu', graduated: false },
41+
{ name: 'Tjebbe', class: 'class32', gitHubName: 'Tjebbee', graduated: true },
42+
];
43+
44+
const teachers = [
45+
{
46+
name: 'Stas',
47+
canTeach: ['javascript', 'browsers', 'using-apis'],
48+
nowTeaching: 'javascript',
49+
},
50+
];
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
'use strict';
2+
/**
3+
* The `state` property says what the traffic light's state (i.e. colour) is at
4+
* that moment.
5+
*/
6+
const trafficLight = {
7+
state: 'red',
8+
};
9+
10+
const currentState = trafficLight.state;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
'use strict';
2+
/**
3+
* The `possibleStates` property define the states (in this case: colours)
4+
* in which the traffic light can be.
5+
* The `stateIndex` property indicates which of the possible states is current.
6+
*/
7+
const trafficLight = {
8+
possibleStates: ['green', 'orange', 'red'],
9+
stateIndex: 0,
10+
};
11+
12+
const currentState = trafficLight.possibleStates[trafficLight.stateIndex];

Week1/prep-exercises/1-objects-and-arrays/traffic-light.js

Lines changed: 0 additions & 20 deletions
This file was deleted.

Week2/prep-exercises/1-traffic-light/traffic-light-1.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1-
"use strict";
1+
'use strict';
22
/**
3-
* Our state variable says what the traffic light's color is at that moment
3+
* The `state` property says what the traffic light's state (i.e. colour) is at
4+
* that moment.
45
*/
56
const trafficLight = {
6-
state: "green",
7+
state: 'green',
78
};
89

910
let rotations = 0;
1011
while (rotations < 2) {
11-
const currentTrafficLightState = trafficLight.state;
12-
console.log("The traffic light is on", currentTrafficLightState);
12+
const currentState = trafficLight.state;
13+
console.log('The traffic light is on', currentState);
1314

1415
// TODO
1516
// if the color is green, turn it orange

Week2/prep-exercises/1-traffic-light/traffic-light-2.js

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
1-
"use strict";
1+
'use strict';
22
/**
3-
* Our state variable says what the traffic light's color is at that moment
3+
* The `possibleStates` property define the states (in this case: colours)
4+
* in which the traffic light can be.
5+
* The `stateIndex` property indicates which of the possible states is current.
46
*/
57
const trafficLight = {
6-
possibleStates: ["green", "orange", "red"],
7-
currentStateIndex: 0,
8+
possibleStates: ['green', 'orange', 'red'],
9+
stateIndex: 0,
810
};
911

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);
12+
let cycle = 0;
13+
while (cycle < 2) {
14+
const currentState = trafficLight.possibleStates[trafficLight.stateIndex];
15+
console.log('The traffic light is on', currentState);
1516

1617
// TODO
1718
// if the color is green, turn it orange
1819
// if the color is orange, turn it red
19-
// if the color is red, add 1 to rotations and turn it green
20+
// if the color is red, add 1 to cycles and turn it green
2021
}
2122

2223
/**
Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,60 @@
1-
"use strict";
1+
'use strict';
22
/**
3-
* Our state variable says what the traffic light's color is at that moment
3+
* The `trafficLight` object is now no longer a global variable. Instead,
4+
* it is defined in function `main()` and passed as a parameter to other
5+
* functions, as and when needed.
46
*/
57

6-
function getCurrentTrafficLightState(trafficLight) {
8+
function getCurrentState(trafficLight) {
79
// TODO
8-
// Should return `green`, `orange` or `red` depending on the state of the given parameter
10+
// Should return the current state (i.e. colour) of the `trafficLight`
11+
// object passed as a parameter.
912
}
1013

1114
function getNextStateIndex(trafficLight) {
1215
// 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+
// Return the index of the next state of the `trafficLight` such that:
17+
// - if the color is green, it will turn to orange
18+
// - if the color is orange, it will turn to red
19+
// - if the color is red, it will turn to green
20+
}
21+
22+
// This function loops for the number of seconds specified by the `secs`
23+
// parameter and then returns.
24+
// IMPORTANT: This is not the recommended way to implement 'waiting' in
25+
// JavaScript. You will learn better ways of doing this when you learn about
26+
// asynchronous code.
27+
function waitSync(secs) {
28+
const start = Date.now();
29+
while (Date.now() - start < secs * 1000) {
30+
// nothing do to here
31+
}
1632
}
1733

1834
function main() {
1935
const trafficLight = {
20-
possibleStates: ["green", "orange", "red"],
21-
currentStateIndex: 0,
36+
possibleStates: ['green', 'orange', 'red'],
37+
stateIndex: 0,
2238
};
2339

24-
for (let i = 0; i < 6; i++) {
25-
const currentTrafficLightState = getCurrentTrafficLightState(trafficLight);
26-
console.log("The traffic light is on", currentTrafficLightState);
40+
for (let cycle = 0; cycle < 6; cycle++) {
41+
const currentState = getCurrentState(trafficLight);
42+
console.log(cycle, 'The traffic light is now', currentState);
2743

28-
trafficLight.currentStateIndex = getNextStateIndex(trafficLight);
44+
waitSync(1); // Wait a second before going to the next state
45+
trafficLight.stateIndex = getNextStateIndex(trafficLight);
2946
}
3047
}
3148

3249
main();
3350
/**
3451
* The output should be:
3552
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
53+
0 The traffic light is now green
54+
1 The traffic light is now orange
55+
2 The traffic light is now red
56+
3 The traffic light is now green
57+
4 The traffic light is now orange
58+
5 The traffic light is now red
4259
4360
*/

Week4/prep-exercises/1-banking/index.js

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,28 +22,43 @@ const bankAccount = {
2222
{
2323
prevAmount: 350,
2424
newAmount: 250,
25-
reason: "Donation",
25+
reason: 'Donation',
2626
},
2727
],
2828
};
29-
const donateMoney = (amount, onSuccess, onFail) => {};
30-
const payRent = (amount, onSuccess, onFail) => {};
29+
30+
const donateMoney = (amount, onSuccess, onFail) => {
31+
// TODO complete this function
32+
};
33+
const payRent = (amount, onSuccess, onFail) => {
34+
// TODO complete this function
35+
};
3136

3237
/**
3338
* TEST CODE. DO NOT EDIT
3439
*/
3540

36-
const onSuccess = () => {
37-
console.log("Payment successful! Thank you!");
41+
const onSuccessEnglish = () => {
42+
console.log('Payment successful! Thank you!');
3843
};
39-
const onFail = () => {
40-
console.log("You do not have enough money to make this payment.");
44+
const onFailEnglish = () => {
45+
console.log('You do not have enough money to make this payment.');
46+
};
47+
48+
const onSuccessDutch = () => {
49+
console.log('Betaling geslaagd! Dank u!');
4150
};
42-
donateMoney(100, onSuccess, onFail);
51+
const onFailDutch = () => {
52+
console.log('U heeft niet voldoende saldo om deze betaling te doen.');
53+
};
54+
55+
donateMoney(100, onSuccessEnglish, onFailEnglish);
4356
console.log(bankAccount);
44-
payRent(100, onSuccess, onFail);
57+
58+
payRent(100, onSuccessEnglish, onFailEnglish);
4559
console.log(bankAccount);
46-
donateMoney(100, onSuccess, onFail);
60+
61+
donateMoney(100, onSuccessDutch, onFailDutch);
4762
console.log(bankAccount);
4863

4964
/**
@@ -66,7 +81,7 @@ transactions: [
6681
{ prevAmount: 150, newAmount: 50, reason: 'Rent' }
6782
]
6883
}
69-
You do not have enough money to make this payment.
84+
U heeft niet voldoende saldo om deze betaling te doen.
7085
{
7186
currentBalance: 50,
7287
transactions: [

0 commit comments

Comments
 (0)