From d9e4268d43fff13a080078d406010498481283b7 Mon Sep 17 00:00:00 2001 From: Jim Cramer Date: Thu, 9 Dec 2021 12:26:18 +0100 Subject: [PATCH] Suggested prep exercise updates --- .../1-objects-and-arrays/school.js | 73 +++++++++++++------ .../1-objects-and-arrays/traffic-light-1.js | 10 +++ .../1-objects-and-arrays/traffic-light-2.js | 12 +++ .../1-objects-and-arrays/traffic-light.js | 20 ----- .../1-traffic-light/traffic-light-1.js | 11 +-- .../1-traffic-light/traffic-light-2.js | 21 +++--- .../1-traffic-light/traffic-light.js | 55 +++++++++----- Week4/prep-exercises/1-banking/index.js | 37 +++++++--- 8 files changed, 150 insertions(+), 89 deletions(-) create mode 100644 Week1/prep-exercises/1-objects-and-arrays/traffic-light-1.js create mode 100644 Week1/prep-exercises/1-objects-and-arrays/traffic-light-2.js delete mode 100644 Week1/prep-exercises/1-objects-and-arrays/traffic-light.js diff --git a/Week1/prep-exercises/1-objects-and-arrays/school.js b/Week1/prep-exercises/1-objects-and-arrays/school.js index 9bcc186..015a5d9 100644 --- a/Week1/prep-exercises/1-objects-and-arrays/school.js +++ b/Week1/prep-exercises/1-objects-and-arrays/school.js @@ -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', + }, +]; diff --git a/Week1/prep-exercises/1-objects-and-arrays/traffic-light-1.js b/Week1/prep-exercises/1-objects-and-arrays/traffic-light-1.js new file mode 100644 index 0000000..483e8f4 --- /dev/null +++ b/Week1/prep-exercises/1-objects-and-arrays/traffic-light-1.js @@ -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; diff --git a/Week1/prep-exercises/1-objects-and-arrays/traffic-light-2.js b/Week1/prep-exercises/1-objects-and-arrays/traffic-light-2.js new file mode 100644 index 0000000..f6c586e --- /dev/null +++ b/Week1/prep-exercises/1-objects-and-arrays/traffic-light-2.js @@ -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]; diff --git a/Week1/prep-exercises/1-objects-and-arrays/traffic-light.js b/Week1/prep-exercises/1-objects-and-arrays/traffic-light.js deleted file mode 100644 index 9bde6c8..0000000 --- a/Week1/prep-exercises/1-objects-and-arrays/traffic-light.js +++ /dev/null @@ -1,20 +0,0 @@ -"use strict"; -/** - * Our state variable says what the traffic light's color is at that moment - */ -const simpleTrafficLight = { - state: "red", -}; -const currentTrafficLightState1 = simpleTrafficLight.state; - -/** - * The possibleStates define the options, then the currentStateIndex indicates where the traffic light is at that moment. - */ -const trafficLightWithStates = { - possibleStates: ["green", "orange", "red"], - currentStateIndex: 0, -}; -const currentTrafficLightState2 = - trafficLightWithStates.possibleStates[ - trafficLightWithStates.currentStateIndex - ]; diff --git a/Week2/prep-exercises/1-traffic-light/traffic-light-1.js b/Week2/prep-exercises/1-traffic-light/traffic-light-1.js index de833a7..c773bdd 100644 --- a/Week2/prep-exercises/1-traffic-light/traffic-light-1.js +++ b/Week2/prep-exercises/1-traffic-light/traffic-light-1.js @@ -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 diff --git a/Week2/prep-exercises/1-traffic-light/traffic-light-2.js b/Week2/prep-exercises/1-traffic-light/traffic-light-2.js index 511919d..9bab85f 100644 --- a/Week2/prep-exercises/1-traffic-light/traffic-light-2.js +++ b/Week2/prep-exercises/1-traffic-light/traffic-light-2.js @@ -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 } /** diff --git a/Week3/prep-exercises/1-traffic-light/traffic-light.js b/Week3/prep-exercises/1-traffic-light/traffic-light.js index 2f1825a..a46dad5 100644 --- a/Week3/prep-exercises/1-traffic-light/traffic-light.js +++ b/Week3/prep-exercises/1-traffic-light/traffic-light.js @@ -1,31 +1,48 @@ -"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); } } @@ -33,11 +50,11 @@ 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 */ diff --git a/Week4/prep-exercises/1-banking/index.js b/Week4/prep-exercises/1-banking/index.js index 1edf731..4f45ea4 100644 --- a/Week4/prep-exercises/1-banking/index.js +++ b/Week4/prep-exercises/1-banking/index.js @@ -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); /** @@ -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: [