From 6da8c1ebfa39f6b2ec675e026d9d91efe7e6b6b4 Mon Sep 17 00:00:00 2001 From: xrustik666 Date: Fri, 26 Jan 2024 22:23:23 +0100 Subject: [PATCH 01/11] traffic-light-1 is done --- .../1-traffic-light/traffic-light-1.js | 14 +++++++++++++- .../1-traffic-light/traffic-light-2.js | 10 ++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/Week1/prep-exercises/1-traffic-light/traffic-light-1.js b/Week1/prep-exercises/1-traffic-light/traffic-light-1.js index f1d9169..50295cc 100644 --- a/Week1/prep-exercises/1-traffic-light/traffic-light-1.js +++ b/Week1/prep-exercises/1-traffic-light/traffic-light-1.js @@ -8,10 +8,22 @@ const trafficLight = { }; let rotations = 0; + while (rotations < 2) { const currentState = trafficLight.state; - console.log("The traffic light is on", currentState); + if (currentState === "green") { + trafficLight.state = "orange"; + } else if (currentState === "orange") { + trafficLight.state = "red"; + } else if (currentState === "red") { + trafficLight.state = "green"; + + rotations++; + } + + 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 diff --git a/Week1/prep-exercises/1-traffic-light/traffic-light-2.js b/Week1/prep-exercises/1-traffic-light/traffic-light-2.js index 8c6ba95..0dc6262 100644 --- a/Week1/prep-exercises/1-traffic-light/traffic-light-2.js +++ b/Week1/prep-exercises/1-traffic-light/traffic-light-2.js @@ -14,6 +14,16 @@ while (cycle < 2) { const currentState = trafficLight.possibleStates[trafficLight.stateIndex]; console.log("The traffic light is on", currentState); + if (currentState == 'green') { + stateIndex = 1; + } else if (currentState == 'orange') { + stateIndex = 2; + } else if (currentState == 'red') { + stateIndex = 0; + + cycle++; + } + // TODO // if the color is green, turn it orange // if the color is orange, turn it red From 1c8782dbda8947a46c7383b869806fbb922fced5 Mon Sep 17 00:00:00 2001 From: xrustik666 Date: Fri, 26 Jan 2024 23:07:44 +0100 Subject: [PATCH 02/11] traffic-light-2 is done --- .../1-traffic-light/traffic-light-2.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Week1/prep-exercises/1-traffic-light/traffic-light-2.js b/Week1/prep-exercises/1-traffic-light/traffic-light-2.js index 0dc6262..a19ef37 100644 --- a/Week1/prep-exercises/1-traffic-light/traffic-light-2.js +++ b/Week1/prep-exercises/1-traffic-light/traffic-light-2.js @@ -14,12 +14,12 @@ while (cycle < 2) { const currentState = trafficLight.possibleStates[trafficLight.stateIndex]; console.log("The traffic light is on", currentState); - if (currentState == 'green') { - stateIndex = 1; - } else if (currentState == 'orange') { - stateIndex = 2; - } else if (currentState == 'red') { - stateIndex = 0; + if (trafficLight.possibleStates[trafficLight.stateIndex] == 'green') { + trafficLight.stateIndex++; + } else if (trafficLight.possibleStates[trafficLight.stateIndex] == 'orange') { + trafficLight.stateIndex++; + } else if (trafficLight.possibleStates[trafficLight.stateIndex] == 'red') { + trafficLight.stateIndex = 0; cycle++; } From 7df4750d5195f57cd0cf8275979467699da022b5 Mon Sep 17 00:00:00 2001 From: xrustik666 Date: Fri, 2 Feb 2024 14:07:02 +0100 Subject: [PATCH 03/11] Week 2 is done --- .../1-traffic-light/traffic-light-1.js | 10 ++++----- .../1-traffic-light/traffic-light.js | 18 ++++++++++------ Week2/prep-exercises/2-experiments/index.js | 21 +++++++++++++++++++ 3 files changed, 38 insertions(+), 11 deletions(-) diff --git a/Week1/prep-exercises/1-traffic-light/traffic-light-1.js b/Week1/prep-exercises/1-traffic-light/traffic-light-1.js index 50295cc..f50aa6b 100644 --- a/Week1/prep-exercises/1-traffic-light/traffic-light-1.js +++ b/Week1/prep-exercises/1-traffic-light/traffic-light-1.js @@ -10,19 +10,19 @@ const trafficLight = { let rotations = 0; while (rotations < 2) { - const currentState = trafficLight.state; + //const currentState = trafficLight.state; - if (currentState === "green") { + if (trafficLight.state === "green") { trafficLight.state = "orange"; - } else if (currentState === "orange") { + } else if (trafficLight.state === "orange") { trafficLight.state = "red"; - } else if (currentState === "red") { + } else if (trafficLight.state === "red") { trafficLight.state = "green"; rotations++; } - console.log("The traffic light is on", currentState); + console.log("The traffic light is on", trafficLight.state); // TODO // if the color is green, turn it orange diff --git a/Week2/prep-exercises/1-traffic-light/traffic-light.js b/Week2/prep-exercises/1-traffic-light/traffic-light.js index f4a5c1a..93b48b8 100644 --- a/Week2/prep-exercises/1-traffic-light/traffic-light.js +++ b/Week2/prep-exercises/1-traffic-light/traffic-light.js @@ -6,17 +6,21 @@ */ function getCurrentState(trafficLight) { - // TODO - // Should return the current state (i.e. colour) of the `trafficLight` - // object passed as a parameter. + // Return the current state (i.e. colour) of the `trafficLight` + return trafficLight.possibleStates[trafficLight.stateIndex]; } function getNextStateIndex(trafficLight) { - // TODO - // Return the index of the next state of the `trafficLight` such that: + // Return the index of the next state of the `trafficLight` // - 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 + + if (trafficLight.stateIndex === trafficLight.possibleStates.length - 1) { + return 0; + } + + return trafficLight.stateIndex + 1; } // This function loops for the number of seconds specified by the `secs` @@ -24,6 +28,7 @@ function getNextStateIndex(trafficLight) { // 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) { @@ -47,6 +52,7 @@ function main() { } main(); + /** * The output should be: @@ -57,4 +63,4 @@ main(); 4 The traffic light is now orange 5 The traffic light is now red -*/ +*/ \ No newline at end of file diff --git a/Week2/prep-exercises/2-experiments/index.js b/Week2/prep-exercises/2-experiments/index.js index 7e5aa92..b058d3f 100644 --- a/Week2/prep-exercises/2-experiments/index.js +++ b/Week2/prep-exercises/2-experiments/index.js @@ -3,6 +3,12 @@ function runExperiment(sampleSize) { const valueCounts = [0, 0, 0, 0, 0, 0]; + for (let i = 0; i < sampleSize; i++) { + const randNum = Math.floor(Math.random() * (6 - 1 + 1)) + 1; + + valueCounts[randNum - 1]++; + } + // TODO // Write a for loop that iterates `sampleSize` times (sampleSize is a number). // In each loop iteration: @@ -13,8 +19,17 @@ function runExperiment(sampleSize) { // for keeping a count how many times the value 1 is thrown, the second // element for value 2, etc. + let count = 0; + const results = []; + for (let valueCount of valueCounts) { + //console.log(`Array value: ${valueCount}`); + + results[count] = (valueCount * 100 / sampleSize).toFixed(2) + "%"; + count++; + } + // TODO // Write a for..of loop for the `valueCounts` array created in the previous // loop. In each loop iteration: @@ -31,6 +46,12 @@ function runExperiment(sampleSize) { function main() { const sampleSizes = [100, 1000, 1000000]; + for (let i = 0; i < sampleSizes.length; i++) { + console.log(`${runExperiment(sampleSizes[i])} | ${sampleSizes[i]}`); + } + + + // TODO // Write a for..of loop that calls the `runExperiment()` function for each // value of the `sampleSizes` array. From e86c2eec23a85ae3dc73dc5f89071c5136797cfc Mon Sep 17 00:00:00 2001 From: xrustik666 Date: Fri, 2 Feb 2024 15:37:07 +0100 Subject: [PATCH 04/11] experiment enhancement --- Week2/prep-exercises/2-experiments/index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Week2/prep-exercises/2-experiments/index.js b/Week2/prep-exercises/2-experiments/index.js index b058d3f..4514ae5 100644 --- a/Week2/prep-exercises/2-experiments/index.js +++ b/Week2/prep-exercises/2-experiments/index.js @@ -46,8 +46,8 @@ function runExperiment(sampleSize) { function main() { const sampleSizes = [100, 1000, 1000000]; - for (let i = 0; i < sampleSizes.length; i++) { - console.log(`${runExperiment(sampleSizes[i])} | ${sampleSizes[i]}`); + for (let sizeElement of sampleSizes) { + console.log(`[${runExperiment(sizeElement)}] ${sizeElement}`); } From c080ecef2378307ef4f7564d16eba64cc5f56fc5 Mon Sep 17 00:00:00 2001 From: xrustik666 Date: Fri, 2 Feb 2024 15:40:38 +0100 Subject: [PATCH 05/11] experiment.js enhancement --- Week2/prep-exercises/2-experiments/index.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Week2/prep-exercises/2-experiments/index.js b/Week2/prep-exercises/2-experiments/index.js index 4514ae5..fe7078d 100644 --- a/Week2/prep-exercises/2-experiments/index.js +++ b/Week2/prep-exercises/2-experiments/index.js @@ -47,11 +47,9 @@ function main() { const sampleSizes = [100, 1000, 1000000]; for (let sizeElement of sampleSizes) { - console.log(`[${runExperiment(sizeElement)}] ${sizeElement}`); + console.log(runExperiment(sizeElement), sizeElement); } - - // TODO // Write a for..of loop that calls the `runExperiment()` function for each // value of the `sampleSizes` array. From aec91f00bac2f9de1b5acded47d90e14e3b03216 Mon Sep 17 00:00:00 2001 From: xrustik666 Date: Fri, 9 Feb 2024 15:02:25 +0100 Subject: [PATCH 06/11] Excersises and challenges are almost completed --- Week2/prep-exercises/2-experiments/index.js | 5 ++- Week3/challenges/1-sum-entries.js | 8 +++++ Week3/challenges/2-sum-three-entries.js | 12 ++++++- .../1-hyf-program/1-find-mentors.js | 23 ++++++++++--- .../1-hyf-program/2-class-list.js | 33 +++++++++++++++++-- 5 files changed, 71 insertions(+), 10 deletions(-) diff --git a/Week2/prep-exercises/2-experiments/index.js b/Week2/prep-exercises/2-experiments/index.js index fe7078d..a0b1eeb 100644 --- a/Week2/prep-exercises/2-experiments/index.js +++ b/Week2/prep-exercises/2-experiments/index.js @@ -19,9 +19,8 @@ function runExperiment(sampleSize) { // for keeping a count how many times the value 1 is thrown, the second // element for value 2, etc. - let count = 0; - const results = []; + let count = 0; for (let valueCount of valueCounts) { //console.log(`Array value: ${valueCount}`); @@ -44,7 +43,7 @@ function runExperiment(sampleSize) { } function main() { - const sampleSizes = [100, 1000, 1000000]; + const sampleSizes = [100, 1000, 1000000, 10000000, , 1000000000]; for (let sizeElement of sampleSizes) { console.log(runExperiment(sizeElement), sizeElement); diff --git a/Week3/challenges/1-sum-entries.js b/Week3/challenges/1-sum-entries.js index f7dd419..7cc055d 100644 --- a/Week3/challenges/1-sum-entries.js +++ b/Week3/challenges/1-sum-entries.js @@ -11,6 +11,14 @@ const list = [1721, 979, 366, 299, 675, 1456]; let result; // Write your code here +for (let i = 0; i < list.length; i++) { + for (let j = i + 1; j < list.length; j++) { + if ((list[i] + list[j]) == 2020) { + console.log(list[i] + " x " + list[j] + " = " + list[i]*list[j]) + result = list[i] * list[j]; + } + } +} // TEST CODE, do not change diff --git a/Week3/challenges/2-sum-three-entries.js b/Week3/challenges/2-sum-three-entries.js index f5f8773..6dbee26 100644 --- a/Week3/challenges/2-sum-three-entries.js +++ b/Week3/challenges/2-sum-three-entries.js @@ -10,7 +10,17 @@ Once you have found those numbers, multiply the numbers and store the result of const list = [1721, 979, 366, 299, 675, 1456]; let result; -// Write your code here +for (let i = 0; i < list.length; i++) { + for (let j = 0; j < list.length; j++) { + for (let k = 0; k < list.length; k++) { + if ((list[i] + list[j] + list[k]) === 2020) { + + result = list[i] * list[j] * list[k]; + console.log(`${list[i]} * ${list[j]} * ${list[k]} = ${result}`); + } + } + } +} // TEST CODE, do not change diff --git a/Week3/prep-exercises/1-hyf-program/1-find-mentors.js b/Week3/prep-exercises/1-hyf-program/1-find-mentors.js index 72baa61..da57a14 100644 --- a/Week3/prep-exercises/1-hyf-program/1-find-mentors.js +++ b/Week3/prep-exercises/1-hyf-program/1-find-mentors.js @@ -8,10 +8,19 @@ import { modules, students, mentors, classes } from "./hyf.js"; * ['John', 'Mary'] */ const possibleMentorsForModule = (moduleName) => { - // TODO complete this function + const jsMentors = []; + for (let i = 0; i < mentors.length; i++) { + if (mentors[i].canTeach.includes(moduleName)) { + jsMentors.push(mentors[i].name); + } + } + + return jsMentors; }; + // You can uncomment out this line to try your function -// console.log(possibleMentorsForModule('using-apis')); +console.log(possibleMentorsForModule('using-apis')); + /** * Tjebbe wants to make it even easier for himself. @@ -20,7 +29,13 @@ const possibleMentorsForModule = (moduleName) => { * It should return a single name. */ const findMentorForModule = (moduleName) => { - // TODO complete this function + const randMentor = Math.floor(Math.random() * (possibleMentorsForModule(moduleName).length)); + const possibleMentors = possibleMentorsForModule(moduleName); + + return possibleMentors[randMentor]; }; + // You can uncomment out this line to try your function -// console.log(findMentorForModule('javascript')); +for (let i = 0; i < 20; i++) { + console.log(findMentorForModule('using-apis')); +} diff --git a/Week3/prep-exercises/1-hyf-program/2-class-list.js b/Week3/prep-exercises/1-hyf-program/2-class-list.js index 44d2798..647d9a5 100644 --- a/Week3/prep-exercises/1-hyf-program/2-class-list.js +++ b/Week3/prep-exercises/1-hyf-program/2-class-list.js @@ -11,11 +11,40 @@ import { modules, students, mentors, classes } from "./hyf.js"; * * [{ name: 'John', role: 'student' }, { name: 'Mary', role: 'mentor' }] */ + +const getModuleName = (className) => { + for (let i = 0; i < classes.length; i++) { + if ('currentModule' in classes[i] && classes[i].name === className) { + return classes[i].currentModule; + } + } + + return 'There is no current module in this class'; +} + +//console.log(getModuleName('class34')); + const getPeopleOfClass = (className) => { - // TODO complete this function + let activePeople = []; + + + for (let i = 0; i < mentors.length; i++) { + if (mentors[i].nowTeaching === getModuleName(className)) { + activePeople.push(mentors[i].name); + } + } + + for (let i = 0; i Date: Fri, 9 Feb 2024 15:21:44 +0100 Subject: [PATCH 07/11] prep excersise improved --- .../1-hyf-program/2-class-list.js | 41 ++++++++++++++----- 1 file changed, 30 insertions(+), 11 deletions(-) diff --git a/Week3/prep-exercises/1-hyf-program/2-class-list.js b/Week3/prep-exercises/1-hyf-program/2-class-list.js index 647d9a5..3491073 100644 --- a/Week3/prep-exercises/1-hyf-program/2-class-list.js +++ b/Week3/prep-exercises/1-hyf-program/2-class-list.js @@ -12,35 +12,54 @@ import { modules, students, mentors, classes } from "./hyf.js"; * [{ name: 'John', role: 'student' }, { name: 'Mary', role: 'mentor' }] */ -const getModuleName = (className) => { - for (let i = 0; i < classes.length; i++) { - if ('currentModule' in classes[i] && classes[i].name === className) { - return classes[i].currentModule; - } - } - return 'There is no current module in this class'; -} //console.log(getModuleName('class34')); const getPeopleOfClass = (className) => { + // This function returns current module name of a class + const getModuleName = (className) => { + for (let i = 0; i < classes.length; i++) { + if ('currentModule' in classes[i] && classes[i].name === className) { + return classes[i].currentModule; + } + } + + return 'There is no current module in this class'; + } + + // List of people who are participating in a class let activePeople = []; - + // Searching mentors who are teaching now for (let i = 0; i < mentors.length; i++) { if (mentors[i].nowTeaching === getModuleName(className)) { - activePeople.push(mentors[i].name); + let mentor = { + name: mentors[i].name, + role: 'mentor' + }; + activePeople.push(mentor); } } + // Searching students who study in a class for (let i = 0; i Date: Fri, 9 Feb 2024 16:27:13 +0100 Subject: [PATCH 08/11] prep excercises are done --- .../1-hyf-program/2-class-list.js | 37 ++++++++++++------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/Week3/prep-exercises/1-hyf-program/2-class-list.js b/Week3/prep-exercises/1-hyf-program/2-class-list.js index 3491073..a8a982e 100644 --- a/Week3/prep-exercises/1-hyf-program/2-class-list.js +++ b/Week3/prep-exercises/1-hyf-program/2-class-list.js @@ -17,7 +17,7 @@ import { modules, students, mentors, classes } from "./hyf.js"; //console.log(getModuleName('class34')); const getPeopleOfClass = (className) => { - // This function returns current module name of a class + // Returns current module name of a class const getModuleName = (className) => { for (let i = 0; i < classes.length; i++) { if ('currentModule' in classes[i] && classes[i].name === className) { @@ -25,7 +25,7 @@ const getPeopleOfClass = (className) => { } } - return 'There is no current module in this class'; + return false; } // List of people who are participating in a class @@ -38,6 +38,7 @@ const getPeopleOfClass = (className) => { name: mentors[i].name, role: 'mentor' }; + activePeople.push(mentor); } } @@ -49,21 +50,18 @@ const getPeopleOfClass = (className) => { name: students[i].name, role: 'student' }; + activePeople.push(student); } } - // Returning the list of participants - return activePeople; - /* - for (let i = 0; i < activePeople.length; i++) { - return `{name: ${activePeople[i]}}`; - } - */ + if (getModuleName (className) === false) { + return "No active people in this class" + } else return activePeople; }; // You can uncomment out this line to try your function -console.log(getPeopleOfClass('class34')); +//console.log(getPeopleOfClass('class34')); /** * We would like to have a complete overview of the current active classes. @@ -78,7 +76,20 @@ console.log(getPeopleOfClass('class34')); * } */ const getActiveClasses = () => { - // TODO complete this function + let activeClasses = []; + + for (let i = 0; i < classes.length; i++) { + if (classes[i].active) { + activeClasses.push(classes[i].name); + } + } + + return activeClasses; }; -// You can uncomment out this line to try your function -// console.log(getActiveClasses()); + +// Show all active people for each active class +const showActiveClasses = getActiveClasses(); + +for (let i = 0; i < showActiveClasses.length; i++) { + console.log(showActiveClasses[i], getPeopleOfClass(showActiveClasses[i])); +} From e0bf73a5fc9a1cd760607ac3a5cc9f6b019f5ee9 Mon Sep 17 00:00:00 2001 From: xrustik666 Date: Sat, 10 Feb 2024 17:23:49 +0100 Subject: [PATCH 09/11] getActiveCLasses enhanced --- Week3/prep-exercises/1-hyf-program/2-class-list.js | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/Week3/prep-exercises/1-hyf-program/2-class-list.js b/Week3/prep-exercises/1-hyf-program/2-class-list.js index a8a982e..91b3cba 100644 --- a/Week3/prep-exercises/1-hyf-program/2-class-list.js +++ b/Week3/prep-exercises/1-hyf-program/2-class-list.js @@ -76,20 +76,15 @@ const getPeopleOfClass = (className) => { * } */ const getActiveClasses = () => { - let activeClasses = []; + let activeClasses = {}; for (let i = 0; i < classes.length; i++) { if (classes[i].active) { - activeClasses.push(classes[i].name); + activeClasses[classes[i].name] = getPeopleOfClass(classes[i].name); } } return activeClasses; }; -// Show all active people for each active class -const showActiveClasses = getActiveClasses(); - -for (let i = 0; i < showActiveClasses.length; i++) { - console.log(showActiveClasses[i], getPeopleOfClass(showActiveClasses[i])); -} +console.log(getActiveClasses()); From 478ccca06525f8a063811cf53428dc77181b8cff Mon Sep 17 00:00:00 2001 From: xrustik666 Date: Sat, 17 Feb 2024 12:51:35 +0100 Subject: [PATCH 10/11] week 4 prep-excersises are done --- Week3/challenges/3-password-validation.js | 12 ++- Week4/prep-exercises/1-wallet/ex2-classes.js | 82 ++++++++++++++++++ Week4/prep-exercises/1-wallet/ex3-object.js | 77 +++++++++++++++++ .../1-wallet/ex4-object-shared-methods.js | 84 +++++++++++++++++++ .../prep-exercises/1-wallet/ex5-prototype.js | 77 +++++++++++++++++ Week4/prep-exercises/1-wallet/package.json | 3 + 6 files changed, 334 insertions(+), 1 deletion(-) create mode 100644 Week4/prep-exercises/1-wallet/package.json diff --git a/Week3/challenges/3-password-validation.js b/Week3/challenges/3-password-validation.js index fa7ad11..e4adc04 100644 --- a/Week3/challenges/3-password-validation.js +++ b/Week3/challenges/3-password-validation.js @@ -20,4 +20,14 @@ const passwordList = [ { times: '1-3', letter: 'a', password: 'abcde'}, { times: '1-3', letter: 'b', password: 'cdefg'}, { times: '2-9', letter: 'c', password: 'ccccccccc'} -]; \ No newline at end of file +]; + +const checkPassword = function(password) { + if (password.password.includes(password.letter) && ) { + return true; + } else return false; +} + +for (let i = 0; i < passwordList.length; i++) { + console.log(checkPassword(passwordList[i])); +} diff --git a/Week4/prep-exercises/1-wallet/ex2-classes.js b/Week4/prep-exercises/1-wallet/ex2-classes.js index f016137..f3a1638 100644 --- a/Week4/prep-exercises/1-wallet/ex2-classes.js +++ b/Week4/prep-exercises/1-wallet/ex2-classes.js @@ -1,5 +1,6 @@ import eurosFormatter from './euroFormatter.js'; +/* class Wallet { #name; #cash; @@ -61,3 +62,84 @@ function main() { } main(); +*/ + +class Wallet { + #name; + #cash; + #dailyAllowance; + #dayTotalWithdrawals; + + constructor(name, cash, dailyAllowance = 40) { + this.#name = name; + this.#cash = cash; + this.#dailyAllowance = dailyAllowance; + this.#dayTotalWithdrawals = 0; + } + + get name() { + return this.#name; + } + + deposit(amount) { + this.#cash += amount; + } + + withdraw(amount) { + if (this.#cash - amount < 0) { + console.log(`Insufficient funds`); + return 0; + } + + if (this.#dayTotalWithdrawals + amount > this.#dailyAllowance) { + console.log(`Insufficient remaining daily allowance!`); + return 0; + } + + this.#cash -= amount; + this.#dayTotalWithdrawals += amount; + return amount; + } + + transferInto(wallet, amount) { + console.log( + `Transferring ${eurosFormatter.format(amount)} from ${this.name} to ${ + wallet.name + }` + ); + const withdrawnAmount = this.withdraw(amount); + wallet.deposit(withdrawnAmount); + } + + reportBalance() { + console.log( + `Name: ${this.name}, balance: ${eurosFormatter.format(this.#cash)}` + ); + } + + resetDailyAllowance() { + this.#dayTotalWithdrawals = 0; + } + + setDailyAllowance(newAllowance) { + this.#dailyAllowance = newAllowance; + } +} + +function main() { + const walletJack = new Wallet('Jack', 100); + const walletJoe = new Wallet('Joe', 10); + const walletJane = new Wallet('Jane', 20); + + walletJack.transferInto(walletJoe, 50); + walletJane.transferInto(walletJoe, 25); + + walletJane.deposit(20); + walletJane.transferInto(walletJoe, 25); + + walletJack.reportBalance(); + walletJoe.reportBalance(); + walletJane.reportBalance(); +} + +main(); \ No newline at end of file diff --git a/Week4/prep-exercises/1-wallet/ex3-object.js b/Week4/prep-exercises/1-wallet/ex3-object.js index e94faac..897303b 100644 --- a/Week4/prep-exercises/1-wallet/ex3-object.js +++ b/Week4/prep-exercises/1-wallet/ex3-object.js @@ -1,5 +1,6 @@ import eurosFormatter from './euroFormatter.js'; +/* function createWallet(name, cash = 0) { return { _name: name, @@ -58,3 +59,79 @@ function main() { } main(); +*/ + +function createWallet(name, cash = 0) { + return { + _name: name, + _cash: cash, + _dailyAllowance: 40, + _dayTotalWithdrawals: 0, + + deposit: function (amount) { + this._cash += amount; + }, + + withdraw: function (amount) { + if (this._cash - amount < 0) { + console.log(`Insufficient funds`); + return 0; + } + + if (this._dayTotalWithdrawals + amount > this._dailyAllowance) { + console.log(`Insufficient remaining daily allowance!`); + return 0; + } + + this._cash -= amount; + this._dayTotalWithdrawals += amount; + return amount; + }, + + transferInto: function (wallet, amount) { + console.log( + `Transferring ${eurosFormatter.format(amount)} from ${ + this._name + } to ${wallet.getName()}` + ); + const withdrawnAmount = this.withdraw(amount); + wallet.deposit(withdrawnAmount); + }, + + reportBalance: function () { + console.log( + `Name: ${this._name}, balance: ${eurosFormatter.format(this._cash)}` + ); + }, + + getName: function () { + return this._name; + }, + + resetDailyAllowance: function () { + this._dayTotalWithdrawals = 0; + }, + + setDailyAllowance: function (newAllowance) { + this._dailyAllowance = newAllowance; + } + }; +} + +function main() { + const walletJack = createWallet('Jack', 100); + const walletJoe = createWallet('Joe', 10); + const walletJane = createWallet('Jane', 20); + + walletJack.transferInto(walletJoe, 50); + walletJane.transferInto(walletJoe, 25); + + walletJane.deposit(20); + walletJane.transferInto(walletJoe, 25); + + walletJack.reportBalance(); + walletJoe.reportBalance(); + walletJane.reportBalance(); +} + +main(); \ No newline at end of file diff --git a/Week4/prep-exercises/1-wallet/ex4-object-shared-methods.js b/Week4/prep-exercises/1-wallet/ex4-object-shared-methods.js index bd4fd20..ea04907 100644 --- a/Week4/prep-exercises/1-wallet/ex4-object-shared-methods.js +++ b/Week4/prep-exercises/1-wallet/ex4-object-shared-methods.js @@ -1,5 +1,6 @@ import eurosFormatter from './euroFormatter.js'; +/* function deposit(amount) { this._cash += amount; } @@ -63,3 +64,86 @@ function main() { } main(); +*/ + +function deposit(amount) { + this._cash += amount; +} + +function withdraw(amount) { + if (this._cash - amount < 0) { + console.log(`Insufficient funds`); + return 0; + } + + if (this._dayTotalWithdrawals + amount > this._dailyAllowance) { + console.log(`Insufficient remaining daily allowance!`); + return 0; + } + + this._cash -= amount; + this._dayTotalWithdrawals += amount; + return amount; +} + +function transferInto(wallet, amount) { + console.log( + `Transferring ${eurosFormatter.format(amount)} from ${ + this._name + } to ${wallet.getName()}` + ); + const withdrawnAmount = this.withdraw(amount); + wallet.deposit(withdrawnAmount); +} + +function reportBalance() { + console.log( + `Name: ${this._name}, balance: ${eurosFormatter.format(this._cash)}` + ); +} + +function resetDailyAllowance() { + this._dayTotalWithdrawals = 0; +} + +function setDailyAllowance(newAllowance) { + this._dailyAllowance = newAllowance; +} + +function getName() { + return this._name; +} + +function createWallet(name, cash = 0) { + return { + _name: name, + _cash: cash, + _dailyAllowance: 40, + _dayTotalWithdrawals: 0, + deposit, + withdraw, + transferInto, + reportBalance, + resetDailyAllowance, + setDailyAllowance, + getName, + }; +} + +function main() { + const walletJack = createWallet('Jack', 100); + const walletJoe = createWallet('Joe', 10); + const walletJane = createWallet('Jane', 20); + + walletJack.transferInto(walletJoe, 50); + walletJane.transferInto(walletJoe, 25); + + walletJane.deposit(20); + walletJane.transferInto(walletJoe, 25); + + walletJack.reportBalance(); + walletJoe.reportBalance(); + walletJane.reportBalance(); +} + +main(); \ No newline at end of file diff --git a/Week4/prep-exercises/1-wallet/ex5-prototype.js b/Week4/prep-exercises/1-wallet/ex5-prototype.js index 7cba410..970bb94 100644 --- a/Week4/prep-exercises/1-wallet/ex5-prototype.js +++ b/Week4/prep-exercises/1-wallet/ex5-prototype.js @@ -1,5 +1,6 @@ import eurosFormatter from './euroFormatter.js'; +/* function Wallet(name, cash) { this._name = name; this._cash = cash; @@ -56,3 +57,79 @@ function main() { } main(); +*/ + +function Wallet(name, cash) { + this._name = name; + this._cash = cash; + this._dailyAllowance = 40; + this._dayTotalWithdrawals = 0; +} + +Wallet.prototype.deposit = function (amount) { + this._cash += amount; +}; + +Wallet.prototype.withdraw = function (amount) { + if (this._cash - amount < 0) { + console.log(`Insufficient funds!`); + return 0; + } + + if (this._dayTotalWithdrawals + amount > this._dailyAllowance) { + console.log(`Insufficient remaining daily allowance!`); + return 0; + } + + this._cash -= amount; + this._dayTotalWithdrawals += amount; + return amount; +}; + +Wallet.prototype.transferInto = function (wallet, amount) { + console.log( + `Transferring ${eurosFormatter.format(amount)} from ${ + this._name + } to ${wallet.getName()}` + ); + const withdrawnAmount = this.withdraw(amount); + if (withdrawnAmount > 0) { + wallet.deposit(withdrawnAmount); + } +}; + +Wallet.prototype.reportBalance = function () { + console.log( + `Name: ${this._name}, balance: ${eurosFormatter.format(this._cash)}` + ); +}; + +Wallet.prototype.getName = function () { + return this._name; +}; + +Wallet.prototype.resetDailyAllowance = function () { + this._dayTotalWithdrawals = 0; +}; + +Wallet.prototype.setDailyAllowance = function (newAllowance) { + this._dailyAllowance = newAllowance; +}; + +function main() { + const walletJack = new Wallet('Jack', 100); + const walletJoe = new Wallet('Joe', 10); + const walletJane = new Wallet('Jane', 20); + + walletJack.transferInto(walletJoe, 50); + walletJane.transferInto(walletJoe, 25); + + walletJane.deposit(20); + walletJane.transferInto(walletJoe, 25); + + walletJack.reportBalance(); + walletJoe.reportBalance(); + walletJane.reportBalance(); +} + +main(); \ No newline at end of file diff --git a/Week4/prep-exercises/1-wallet/package.json b/Week4/prep-exercises/1-wallet/package.json new file mode 100644 index 0000000..3dbc1ca --- /dev/null +++ b/Week4/prep-exercises/1-wallet/package.json @@ -0,0 +1,3 @@ +{ + "type": "module" +} From cd4a0bfb13d652260632347938d93614871af356 Mon Sep 17 00:00:00 2001 From: xrustik666 Date: Sat, 17 Feb 2024 23:39:25 +0100 Subject: [PATCH 11/11] old code deleted --- Week4/prep-exercises/1-wallet/ex2-classes.js | 64 ------------------ Week4/prep-exercises/1-wallet/ex3-object.js | 61 ----------------- .../1-wallet/ex4-object-shared-methods.js | 66 ------------------- .../prep-exercises/1-wallet/ex5-prototype.js | 59 ----------------- 4 files changed, 250 deletions(-) diff --git a/Week4/prep-exercises/1-wallet/ex2-classes.js b/Week4/prep-exercises/1-wallet/ex2-classes.js index f3a1638..4f64262 100644 --- a/Week4/prep-exercises/1-wallet/ex2-classes.js +++ b/Week4/prep-exercises/1-wallet/ex2-classes.js @@ -1,69 +1,5 @@ import eurosFormatter from './euroFormatter.js'; -/* -class Wallet { - #name; - #cash; - - constructor(name, cash) { - this.#name = name; - this.#cash = cash; - } - - get name() { - return this.#name; - } - - deposit(amount) { - this.#cash += amount; - } - - withdraw(amount) { - if (this.#cash - amount < 0) { - console.log(`Insufficient funds!`); - return 0; - } - - this.#cash -= amount; - return amount; - } - - transferInto(wallet, amount) { - console.log( - `Transferring ${eurosFormatter.format(amount)} from ${this.name} to ${ - wallet.name - }` - ); - const withdrawnAmount = this.withdraw(amount); - wallet.deposit(withdrawnAmount); - } - - reportBalance() { - console.log( - `Name: ${this.name}, balance: ${eurosFormatter.format(this.#cash)}` - ); - } -} - -function main() { - const walletJack = new Wallet('Jack', 100); - const walletJoe = new Wallet('Joe', 10); - const walletJane = new Wallet('Jane', 20); - - walletJack.transferInto(walletJoe, 50); - walletJane.transferInto(walletJoe, 25); - - walletJane.deposit(20); - walletJane.transferInto(walletJoe, 25); - - walletJack.reportBalance(); - walletJoe.reportBalance(); - walletJane.reportBalance(); -} - -main(); -*/ - class Wallet { #name; #cash; diff --git a/Week4/prep-exercises/1-wallet/ex3-object.js b/Week4/prep-exercises/1-wallet/ex3-object.js index 897303b..5b80b72 100644 --- a/Week4/prep-exercises/1-wallet/ex3-object.js +++ b/Week4/prep-exercises/1-wallet/ex3-object.js @@ -1,66 +1,5 @@ import eurosFormatter from './euroFormatter.js'; -/* -function createWallet(name, cash = 0) { - return { - _name: name, - _cash: cash, - - deposit: function (amount) { - this._cash += amount; - }, - - withdraw: function (amount) { - if (this._cash - amount < 0) { - console.log(`Insufficient funds!`); - return 0; - } - - this._cash -= amount; - return amount; - }, - - transferInto: function (wallet, amount) { - console.log( - `Transferring ${eurosFormatter.format(amount)} from ${ - this._name - } to ${wallet.getName()}` - ); - const withdrawnAmount = this.withdraw(amount); - wallet.deposit(withdrawnAmount); - }, - - reportBalance: function () { - console.log( - `Name: ${this._name}, balance: ${eurosFormatter.format(this._cash)}` - ); - }, - - getName: function () { - return this._name; - }, - }; -} - -function main() { - const walletJack = createWallet('Jack', 100); - const walletJoe = createWallet('Joe', 10); - const walletJane = createWallet('Jane', 20); - - walletJack.transferInto(walletJoe, 50); - walletJane.transferInto(walletJoe, 25); - - walletJane.deposit(20); - walletJane.transferInto(walletJoe, 25); - - walletJack.reportBalance(); - walletJoe.reportBalance(); - walletJane.reportBalance(); -} - -main(); -*/ - function createWallet(name, cash = 0) { return { _name: name, diff --git a/Week4/prep-exercises/1-wallet/ex4-object-shared-methods.js b/Week4/prep-exercises/1-wallet/ex4-object-shared-methods.js index ea04907..87f6756 100644 --- a/Week4/prep-exercises/1-wallet/ex4-object-shared-methods.js +++ b/Week4/prep-exercises/1-wallet/ex4-object-shared-methods.js @@ -1,71 +1,5 @@ import eurosFormatter from './euroFormatter.js'; -/* -function deposit(amount) { - this._cash += amount; -} - -function withdraw(amount) { - if (this._cash - amount < 0) { - console.log(`Insufficient funds!`); - return 0; - } - - this._cash -= amount; - return amount; -} - -function transferInto(wallet, amount) { - console.log( - `Transferring ${eurosFormatter.format(amount)} from ${ - this._name - } to ${wallet.getName()}` - ); - const withdrawnAmount = this.withdraw(amount); - wallet.deposit(withdrawnAmount); -} - -function reportBalance() { - console.log( - `Name: ${this._name}, balance: ${eurosFormatter.format(this._cash)}` - ); -} - -function getName() { - return this._name; -} - -function createWallet(name, cash = 0) { - return { - _name: name, - _cash: cash, - deposit, - withdraw, - transferInto, - reportBalance, - getName, - }; -} - -function main() { - const walletJack = createWallet('Jack', 100); - const walletJoe = createWallet('Joe', 10); - const walletJane = createWallet('Jane', 20); - - walletJack.transferInto(walletJoe, 50); - walletJane.transferInto(walletJoe, 25); - - walletJane.deposit(20); - walletJane.transferInto(walletJoe, 25); - - walletJack.reportBalance(); - walletJoe.reportBalance(); - walletJane.reportBalance(); -} - -main(); -*/ - function deposit(amount) { this._cash += amount; } diff --git a/Week4/prep-exercises/1-wallet/ex5-prototype.js b/Week4/prep-exercises/1-wallet/ex5-prototype.js index 970bb94..f8036f2 100644 --- a/Week4/prep-exercises/1-wallet/ex5-prototype.js +++ b/Week4/prep-exercises/1-wallet/ex5-prototype.js @@ -1,64 +1,5 @@ import eurosFormatter from './euroFormatter.js'; -/* -function Wallet(name, cash) { - this._name = name; - this._cash = cash; -} - -Wallet.prototype.deposit = function (amount) { - this._cash += amount; -}; - -Wallet.prototype.withdraw = function (amount) { - if (this._cash - amount < 0) { - console.log(`Insufficient funds!`); - return 0; - } - - this._cash -= amount; - return amount; -}; - -Wallet.prototype.transferInto = function (wallet, amount) { - console.log( - `Transferring ${eurosFormatter.format(amount)} from ${ - this._name - } to ${wallet.getName()}` - ); - const withdrawnAmount = this.withdraw(amount); - wallet.deposit(withdrawnAmount); -}; - -Wallet.prototype.reportBalance = function () { - console.log( - `Name: ${this._name}, balance: ${eurosFormatter.format(this._cash)}` - ); -}; - -Wallet.prototype.getName = function () { - return this._name; -}; - -function main() { - const walletJack = new Wallet('Jack', 100); - const walletJoe = new Wallet('Joe', 10); - const walletJane = new Wallet('Jane', 20); - - walletJack.transferInto(walletJoe, 50); - walletJane.transferInto(walletJoe, 25); - - walletJane.deposit(20); - walletJane.transferInto(walletJoe, 25); - - walletJack.reportBalance(); - walletJoe.reportBalance(); - walletJane.reportBalance(); -} - -main(); -*/ - function Wallet(name, cash) { this._name = name; this._cash = cash;