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

Skip to content

Suggested prep exercise updates #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 49 additions & 24 deletions Week1/prep-exercises/1-objects-and-arrays/school.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,50 @@
"use strict";
/**
* 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.
*/
const school1 = {
students: [
{ name: "Rob", class: 1 },
{ name: "Fede", class: 2 },
],
teachers: [{ name: "Stas", class: 1 }],
};
const modules = [
{ name: 'html-css', displayName: 'HTML/CSS' },
{ name: 'javascript', displayName: 'JavaScript' },
{ name: 'browsers', displayName: 'Browsers' },
{ name: 'using-apis', displayName: 'Using APIs' },
{ name: 'node', displayName: 'Node.js' },
{ name: 'databases', displayName: 'Databases' },
{ name: 'react', displayName: 'React' },
{ name: 'project', displayName: 'Project' },
];

/**
* In this representation we have the classes property that has an array per class of everyone in that class.
* 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
*/
const school2 = {
classes: [
[
{ name: "Rob", role: "student" },
{ name: "Stas", role: "teacher" },
],
[{ name: "Fede", role: "student" }],
],
};
const classes = [
{
name: 'class32',
startDate: '23-3-2021',
active: false,
graduationDate: '7-11-2021',
},
{
name: 'class33',
startDate: '28-5-2021',
active: true,
currentModule: 'project',
},
{
name: 'class34',
startDate: '2-9-2021',
active: true,
currentModule: 'node',
},
{
name: 'class35',
startDate: '14-11-2021',
active: true,
currentModule: 'javascript',
},
];

const students = [
{ name: 'Fede', class: 'class34', gitHubName: 'fedefu', graduated: false },
{ name: 'Tjebbe', class: 'class32', gitHubName: 'Tjebbee', graduated: true },
];

const teachers = [
{
name: 'Stas',
canTeach: ['javascript', 'browsers', 'using-apis'],
nowTeaching: 'javascript',
},
];
10 changes: 10 additions & 0 deletions Week1/prep-exercises/1-objects-and-arrays/traffic-light-1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'use strict';
/**
* The `state` property says what the traffic light's state (i.e. colour) is at
* that moment.
*/
const trafficLight = {
state: 'red',
};

const currentState = trafficLight.state;
12 changes: 12 additions & 0 deletions Week1/prep-exercises/1-objects-and-arrays/traffic-light-2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict';
/**
* The `possibleStates` property define the states (in this case: colours)
* in which the traffic light can be.
* The `stateIndex` property indicates which of the possible states is current.
*/
const trafficLight = {
possibleStates: ['green', 'orange', 'red'],
stateIndex: 0,
};

const currentState = trafficLight.possibleStates[trafficLight.stateIndex];
20 changes: 0 additions & 20 deletions Week1/prep-exercises/1-objects-and-arrays/traffic-light.js

This file was deleted.

11 changes: 6 additions & 5 deletions Week2/prep-exercises/1-traffic-light/traffic-light-1.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
"use strict";
'use strict';
/**
* Our state variable says what the traffic light's color is at that moment
* The `state` property says what the traffic light's state (i.e. colour) is at
* that moment.
*/
const trafficLight = {
state: "green",
state: 'green',
};

let rotations = 0;
while (rotations < 2) {
const currentTrafficLightState = trafficLight.state;
console.log("The traffic light is on", currentTrafficLightState);
const currentState = trafficLight.state;
console.log('The traffic light is on', currentState);

// TODO
// if the color is green, turn it orange
Expand Down
21 changes: 11 additions & 10 deletions Week2/prep-exercises/1-traffic-light/traffic-light-2.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
"use strict";
'use strict';
/**
* Our state variable says what the traffic light's color is at that moment
* The `possibleStates` property define the states (in this case: colours)
* in which the traffic light can be.
* The `stateIndex` property indicates which of the possible states is current.
*/
const trafficLight = {
possibleStates: ["green", "orange", "red"],
currentStateIndex: 0,
possibleStates: ['green', 'orange', 'red'],
stateIndex: 0,
};

let rotations = 0;
while (rotations < 2) {
const currentTrafficLightState =
trafficLight.possibleStates[trafficLight.currentStateIndex];
console.log("The traffic light is on", currentTrafficLightState);
let cycle = 0;
while (cycle < 2) {
const currentState = trafficLight.possibleStates[trafficLight.stateIndex];
console.log('The traffic light is on', currentState);

// TODO
// if the color is green, turn it orange
// if the color is orange, turn it red
// if the color is red, add 1 to rotations and turn it green
// if the color is red, add 1 to cycles and turn it green
}

/**
Expand Down
55 changes: 36 additions & 19 deletions Week3/prep-exercises/1-traffic-light/traffic-light.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,60 @@
"use strict";
'use strict';
/**
* Our state variable says what the traffic light's color is at that moment
* The `trafficLight` object is now no longer a global variable. Instead,
* it is defined in function `main()` and passed as a parameter to other
* functions, as and when needed.
*/

function getCurrentTrafficLightState(trafficLight) {
function getCurrentState(trafficLight) {
// TODO
// Should return `green`, `orange` or `red` depending on the state of the given parameter
// Should return the current state (i.e. colour) of the `trafficLight`
// object passed as a parameter.
}

function getNextStateIndex(trafficLight) {
// TODO
// if the color is green, turn it orange
// if the color is orange, turn it red
// if the color is red, turn it green
// Return the index of the next state of the `trafficLight` such that:
// - if the color is green, it will turn to orange
// - if the color is orange, it will turn to red
// - if the color is red, it will turn to green
}

// This function loops for the number of seconds specified by the `secs`
// parameter and then returns.
// IMPORTANT: This is not the recommended way to implement 'waiting' in
// JavaScript. You will learn better ways of doing this when you learn about
// asynchronous code.
function waitSync(secs) {
const start = Date.now();
while (Date.now() - start < secs * 1000) {
// nothing do to here
}
}

function main() {
const trafficLight = {
possibleStates: ["green", "orange", "red"],
currentStateIndex: 0,
possibleStates: ['green', 'orange', 'red'],
stateIndex: 0,
};

for (let i = 0; i < 6; i++) {
const currentTrafficLightState = getCurrentTrafficLightState(trafficLight);
console.log("The traffic light is on", currentTrafficLightState);
for (let cycle = 0; cycle < 6; cycle++) {
const currentState = getCurrentState(trafficLight);
console.log(cycle, 'The traffic light is now', currentState);

trafficLight.currentStateIndex = getNextStateIndex(trafficLight);
waitSync(1); // Wait a second before going to the next state
trafficLight.stateIndex = getNextStateIndex(trafficLight);
}
}

main();
/**
* The output should be:

The traffic light is on green
The traffic light is on orange
The traffic light is on red
The traffic light is on green
The traffic light is on orange
The traffic light is on red
0 The traffic light is now green
1 The traffic light is now orange
2 The traffic light is now red
3 The traffic light is now green
4 The traffic light is now orange
5 The traffic light is now red

*/
37 changes: 26 additions & 11 deletions Week4/prep-exercises/1-banking/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,43 @@ const bankAccount = {
{
prevAmount: 350,
newAmount: 250,
reason: "Donation",
reason: 'Donation',
},
],
};
const donateMoney = (amount, onSuccess, onFail) => {};
const payRent = (amount, onSuccess, onFail) => {};

const donateMoney = (amount, onSuccess, onFail) => {
// TODO complete this function
};
const payRent = (amount, onSuccess, onFail) => {
// TODO complete this function
};

/**
* TEST CODE. DO NOT EDIT
*/

const onSuccess = () => {
console.log("Payment successful! Thank you!");
const onSuccessEnglish = () => {
console.log('Payment successful! Thank you!');
};
const onFail = () => {
console.log("You do not have enough money to make this payment.");
const onFailEnglish = () => {
console.log('You do not have enough money to make this payment.');
};

const onSuccessDutch = () => {
console.log('Betaling geslaagd! Dank u!');
};
donateMoney(100, onSuccess, onFail);
const onFailDutch = () => {
console.log('U heeft niet voldoende saldo om deze betaling te doen.');
};

donateMoney(100, onSuccessEnglish, onFailEnglish);
console.log(bankAccount);
payRent(100, onSuccess, onFail);

payRent(100, onSuccessEnglish, onFailEnglish);
console.log(bankAccount);
donateMoney(100, onSuccess, onFail);

donateMoney(100, onSuccessDutch, onFailDutch);
console.log(bankAccount);

/**
Expand All @@ -66,7 +81,7 @@ transactions: [
{ prevAmount: 150, newAmount: 50, reason: 'Rent' }
]
}
You do not have enough money to make this payment.
U heeft niet voldoende saldo om deze betaling te doen.
{
currentBalance: 50,
transactions: [
Expand Down