From ec47026d0226c26834b274e4d0bebae0efae9dca Mon Sep 17 00:00:00 2001 From: Tenzin Kunchok <145247192+Tenzijn@users.noreply.github.com> Date: Sun, 29 Oct 2023 23:42:52 +0100 Subject: [PATCH 01/10] Update traffic-light-1.js changed the code to required output --- .../prep-exercises/1-traffic-light/traffic-light-1.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 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 f1d9169..4624fd4 100644 --- a/Week1/prep-exercises/1-traffic-light/traffic-light-1.js +++ b/Week1/prep-exercises/1-traffic-light/traffic-light-1.js @@ -3,7 +3,7 @@ * The `state` property says what the traffic light's state (i.e. colour) is at * that moment. */ -const trafficLight = { +let trafficLight = { state: "green", }; @@ -11,7 +11,14 @@ 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{ + rotations++; + trafficLight.state = "green"; +} // TODO // if the color is green, turn it orange // if the color is orange, turn it red From 64c7f039eb45f74528fdcfa2d8dda7b4d192298b Mon Sep 17 00:00:00 2001 From: Tenzin Kunchok <145247192+Tenzijn@users.noreply.github.com> Date: Sun, 29 Oct 2023 23:51:14 +0100 Subject: [PATCH 02/10] Update traffic-light-2.js edited code to required output --- Week1/prep-exercises/1-traffic-light/traffic-light-2.js | 8 ++++++++ 1 file changed, 8 insertions(+) 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..0b3ed8a 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,14 @@ while (cycle < 2) { const currentState = trafficLight.possibleStates[trafficLight.stateIndex]; console.log("The traffic light is on", currentState); +if(currentState == "green"){ + trafficLight.stateIndex ++; +}else if(currentState == "orange") { + trafficLight.stateIndex ++; +}else{ + trafficLight.stateIndex -= 2; + cycle++; +} // TODO // if the color is green, turn it orange // if the color is orange, turn it red From 9d808aabfcba2bf9aaa597807df3ea0207755c25 Mon Sep 17 00:00:00 2001 From: tenzijn Date: Tue, 31 Oct 2023 22:42:13 +0100 Subject: [PATCH 03/10] Prep exercises is completed --- .../1-traffic-light/traffic-light.js | 8 ++-- Week2/prep-exercises/2-experiments/index.html | 17 +++++++ Week2/prep-exercises/2-experiments/index.js | 46 +++++++++++++++---- 3 files changed, 60 insertions(+), 11 deletions(-) create mode 100644 Week2/prep-exercises/2-experiments/index.html diff --git a/Week2/prep-exercises/1-traffic-light/traffic-light.js b/Week2/prep-exercises/1-traffic-light/traffic-light.js index f4a5c1a..b3a020d 100644 --- a/Week2/prep-exercises/1-traffic-light/traffic-light.js +++ b/Week2/prep-exercises/1-traffic-light/traffic-light.js @@ -1,4 +1,4 @@ -"use strict"; +'use strict'; /** * The `trafficLight` object is now no longer a global variable. Instead, * it is defined in function `main()` and passed as a parameter to other @@ -6,12 +6,14 @@ */ function getCurrentState(trafficLight) { + return trafficLight.possibleStates[trafficLight.stateIndex]; // TODO // Should return the current state (i.e. colour) of the `trafficLight` // object passed as a parameter. } function getNextStateIndex(trafficLight) { + return trafficLight.stateIndex == 2 ? 0 : ++trafficLight.stateIndex; // TODO // Return the index of the next state of the `trafficLight` such that: // - if the color is green, it will turn to orange @@ -33,13 +35,13 @@ function waitSync(secs) { function main() { const trafficLight = { - possibleStates: ["green", "orange", "red"], + possibleStates: ['green', 'orange', 'red'], stateIndex: 0, }; for (let cycle = 0; cycle < 6; cycle++) { const currentState = getCurrentState(trafficLight); - console.log(cycle, "The traffic light is now", currentState); + console.log(cycle, 'The traffic light is now', currentState); waitSync(1); // Wait a second before going to the next state trafficLight.stateIndex = getNextStateIndex(trafficLight); diff --git a/Week2/prep-exercises/2-experiments/index.html b/Week2/prep-exercises/2-experiments/index.html new file mode 100644 index 0000000..04d98a3 --- /dev/null +++ b/Week2/prep-exercises/2-experiments/index.html @@ -0,0 +1,17 @@ + + + + + + Codestin Search App + + +

Dice experimentation

+

Results :

+ +

+

+

+ + + diff --git a/Week2/prep-exercises/2-experiments/index.js b/Week2/prep-exercises/2-experiments/index.js index 7e5aa92..ba649c0 100644 --- a/Week2/prep-exercises/2-experiments/index.js +++ b/Week2/prep-exercises/2-experiments/index.js @@ -1,8 +1,20 @@ -"use strict"; +'use strict'; +// new function is created for code reusable. +// This function will give you random number from 1 to 6 +function rollDice(min, max) { + return Math.floor(Math.random() * (max - min + 1)) + 1; +} + +function percent(diceRolled, totalDiceRoll) { + return (diceRolled / totalDiceRoll) * 100; +} function runExperiment(sampleSize) { const valueCounts = [0, 0, 0, 0, 0, 0]; - + for (let index = sampleSize; index > 0; --index) { + let result = rollDice(1, 6); + valueCounts[result - 1] += 1; + } // TODO // Write a for loop that iterates `sampleSize` times (sampleSize is a number). // In each loop iteration: @@ -14,7 +26,14 @@ function runExperiment(sampleSize) { // element for value 2, etc. const results = []; - + valueCounts.forEach((rolledDiceCount) => { + // console.log(percent(rolledDiceCount, sampleSize)); + results.push( + ` '${parseFloat(percent(rolledDiceCount, sampleSize)) + .toFixed(2) + .toString()}'` + ); + }); // TODO // Write a for..of loop for the `valueCounts` array created in the previous // loop. In each loop iteration: @@ -31,13 +50,24 @@ function runExperiment(sampleSize) { function main() { const sampleSizes = [100, 1000, 1000000]; + for (let index = 0; index < sampleSizes.length; index++) { + console.log( + `[ ${runExperiment(sampleSizes[index])} ] ${sampleSizes[index]}` + ); + + // This just extra to print the result on the webpage + document.getElementById( + (index + 1).toString() + ).innerHTML = `[ ${runExperiment(sampleSizes[index])} ] ${ + sampleSizes[index] + }`; + } // TODO - // Write a for..of loop that calls the `runExperiment()` function for each - // value of the `sampleSizes` array. - // Log the results of each experiment as well as the experiment size to the - // console. + // Write a for..of loop + // that calls the `runExperiment()` function for each value of the `sampleSizes` array. + // Log the results of each experiment as well as the experiment size to the console. // The expected output could look like this: - // + // [ '26.00', '17.00', '10.00', '19.00', '16.00', '12.00' ] 100 // [ '14.60', '17.10', '19.30', '15.50', '16.70', '16.80' ] 1000 // [ '16.71', '16.68', '16.69', '16.66', '16.67', '16.59' ] 1000000 From c5f06dd5c0c381bbb9fe3a73968e53a97cd016fd Mon Sep 17 00:00:00 2001 From: tenzijn Date: Fri, 3 Nov 2023 13:12:45 +0100 Subject: [PATCH 04/10] code modified to ternary operator --- .../1-traffic-light/traffic-light-1.js | 17 +++++++++--- .../1-traffic-light/traffic-light-2.js | 27 +++++++++++-------- 2 files changed, 30 insertions(+), 14 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 4624fd4..2f55549 100644 --- a/Week1/prep-exercises/1-traffic-light/traffic-light-1.js +++ b/Week1/prep-exercises/1-traffic-light/traffic-light-1.js @@ -1,16 +1,25 @@ -"use strict"; +'use strict'; /** * The `state` property says what the traffic light's state (i.e. colour) is at * that moment. */ let trafficLight = { - state: "green", + state: 'green', }; let rotations = 0; while (rotations < 2) { const currentState = trafficLight.state; - console.log("The traffic light is on", currentState); + console.log('The traffic light is on', currentState); + + //single line solution + currentState == 'green' + ? (trafficLight.state = 'orange') + : currentState == 'orange' + ? (trafficLight.state = 'red') + : `(${(trafficLight.state = 'green')} ${rotations++})`; + + /* if(currentState == "green"){ trafficLight.state = "orange"; } else if(currentState == "orange"){ @@ -19,6 +28,8 @@ if(currentState == "green"){ rotations++; trafficLight.state = "green"; } +*/ + // 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 0b3ed8a..acd8ecb 100644 --- a/Week1/prep-exercises/1-traffic-light/traffic-light-2.js +++ b/Week1/prep-exercises/1-traffic-light/traffic-light-2.js @@ -1,27 +1,32 @@ -"use strict"; +'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"], + possibleStates: ['green', 'orange', 'red'], stateIndex: 0, }; let cycle = 0; while (cycle < 2) { const currentState = trafficLight.possibleStates[trafficLight.stateIndex]; - console.log("The traffic light is on", currentState); + console.log('The traffic light is on', currentState); + // single line solution + currentState == 'green' || currentState == 'orange' + ? trafficLight.stateIndex++ + : `${(trafficLight.stateIndex -= 2)} ${cycle++}`; + + /* + if (currentState == 'green' || currentState == 'orange') { + trafficLight.stateIndex++; + } else { + trafficLight.stateIndex -= 2; + cycle++; + } + */ -if(currentState == "green"){ - trafficLight.stateIndex ++; -}else if(currentState == "orange") { - trafficLight.stateIndex ++; -}else{ - trafficLight.stateIndex -= 2; - cycle++; -} // TODO // if the color is green, turn it orange // if the color is orange, turn it red From 748731fb4214acff5d2c7f675b2511210c40ad11 Mon Sep 17 00:00:00 2001 From: Tenzin Kunchok <145247192+Tenzijn@users.noreply.github.com> Date: Wed, 8 Nov 2023 11:57:41 +0100 Subject: [PATCH 05/10] completed week 3 --- .../1-hyf-program/1-find-mentors.js | 26 +++++++++++++++---- .../1-hyf-program/2-class-list.js | 24 ++++++++++++++--- 2 files changed, 42 insertions(+), 8 deletions(-) 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..d60a93c 100644 --- a/Week3/prep-exercises/1-hyf-program/1-find-mentors.js +++ b/Week3/prep-exercises/1-hyf-program/1-find-mentors.js @@ -1,4 +1,4 @@ -import { modules, students, mentors, classes } from "./hyf.js"; +import { modules, students, mentors, classes } from './hyf.js'; /** * Tjebbe would like help to get a list of possible mentors for a module. @@ -8,10 +8,16 @@ import { modules, students, mentors, classes } from "./hyf.js"; * ['John', 'Mary'] */ const possibleMentorsForModule = (moduleName) => { - // TODO complete this function + const mentorList = []; + mentors.forEach((mentor) => { + if (mentor.canTeach.includes(moduleName)) { + mentorList.push(mentor.name); + } + }); + return mentorList; }; // You can uncomment out this line to try your function -// console.log(possibleMentorsForModule('using-apis')); +console.log(`${possibleMentorsForModule('using-apis')} can teach API module`); /** * Tjebbe wants to make it even easier for himself. @@ -20,7 +26,17 @@ const possibleMentorsForModule = (moduleName) => { * It should return a single name. */ const findMentorForModule = (moduleName) => { - // TODO complete this function + const mentorList = []; + let randomNum; + mentors.forEach((mentor) => { + if (mentor.canTeach.includes(moduleName)) { + mentorList.push(mentor.name); + } + }); + randomNum = Math.floor(Math.random() * (mentorList.length - 1 - 0 + 1)) + 0; + return mentorList[randomNum]; }; // You can uncomment out this line to try your function -// console.log(findMentorForModule('javascript')); +console.log( + `${findMentorForModule('javascript')} is going to teach Javascript` +); 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..2709c43 100644 --- a/Week3/prep-exercises/1-hyf-program/2-class-list.js +++ b/Week3/prep-exercises/1-hyf-program/2-class-list.js @@ -1,4 +1,4 @@ -import { modules, students, mentors, classes } from "./hyf.js"; +import { modules, students, mentors, classes } from './hyf.js'; /** * We would like to have a list of everyone that is currently participating in a class. @@ -12,10 +12,28 @@ import { modules, students, mentors, classes } from "./hyf.js"; * [{ name: 'John', role: 'student' }, { name: 'Mary', role: 'mentor' }] */ const getPeopleOfClass = (className) => { - // TODO complete this function + let everyone = []; + let currentModule; + + classes.forEach((eachClass) => { + if (eachClass.name == className) currentModule = eachClass.currentModule; + }); + + students.forEach((stud) => { + if (stud.class == className) { + everyone.push({ name: stud.name, role: 'student' }); + } + }); + + mentors.forEach((mentor) => { + if (mentor.nowTeaching == currentModule) + everyone.push({ name: mentor.name, role: 'mentor' }); + }); + + return everyone; }; // 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. From 88c2d3c242b07ba13fbb3fcd4fa3342daf8086c8 Mon Sep 17 00:00:00 2001 From: tenzijn Date: Wed, 8 Nov 2023 17:58:34 +0100 Subject: [PATCH 06/10] week one exercise is done --- Week1/practice-exercises/1-remove-the-comma.js | 14 ++++++++++---- Week1/practice-exercises/2-even-odd-reporter.js | 6 +++++- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/Week1/practice-exercises/1-remove-the-comma.js b/Week1/practice-exercises/1-remove-the-comma.js index b71cffd..d315aec 100644 --- a/Week1/practice-exercises/1-remove-the-comma.js +++ b/Week1/practice-exercises/1-remove-the-comma.js @@ -1,14 +1,20 @@ /** * We want to remove the comma's in the given string (myString), replace them with a space and log it to the console. - * - * The end result should be: + * + * The end result should be: * hello this is a difficult to read sentence */ let myString = 'hello,this,is,a,difficult,to,read,sentence'; - +// solution 1 +myString = myString.replaceAll(',', ' '); +// solution 2 +// myString = myString.split(',').join(' '); /* --- Code that will test your solution, do NOT change. Write above this line --- */ -console.assert(myString === 'hello this is a difficult to read sentence', 'There is something wrong with your solution'); \ No newline at end of file +console.assert( + myString === 'hello this is a difficult to read sentence', + 'There is something wrong with your solution' +); diff --git a/Week1/practice-exercises/2-even-odd-reporter.js b/Week1/practice-exercises/2-even-odd-reporter.js index 6edf23e..a1ebe6e 100644 --- a/Week1/practice-exercises/2-even-odd-reporter.js +++ b/Week1/practice-exercises/2-even-odd-reporter.js @@ -6,4 +6,8 @@ * If it's odd, log to the console The number [PUT_NUMBER_HERE] is odd!. * If it's even, log to the console The number [PUT_NUMBER_HERE] is even!. */ - +for (let number = 0; number <= 20; number++) { + number % 2 == 0 + ? console.log(`The number ${number} is Even!`) + : console.log(`The number ${number} is odd!`); +} From dc9c46b94e4362f01a73d64ef6208481392465e1 Mon Sep 17 00:00:00 2001 From: tenzijn Date: Sat, 18 Nov 2023 10:59:05 +0100 Subject: [PATCH 07/10] week3 completed --- .../1-hyf-program/1-find-mentors.js | 19 ++++++-- .../1-hyf-program/2-class-list.js | 45 ++++++++++++++++++- 2 files changed, 60 insertions(+), 4 deletions(-) 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 d60a93c..c12e221 100644 --- a/Week3/prep-exercises/1-hyf-program/1-find-mentors.js +++ b/Week3/prep-exercises/1-hyf-program/1-find-mentors.js @@ -7,7 +7,11 @@ import { modules, students, mentors, classes } from './hyf.js'; * It should return an array of names. So something like: * ['John', 'Mary'] */ + +// then we take the name of the teacher and put it in a new array + const possibleMentorsForModule = (moduleName) => { + // first we take all the teacher who can teach the 'using apis' module const mentorList = []; mentors.forEach((mentor) => { if (mentor.canTeach.includes(moduleName)) { @@ -17,7 +21,7 @@ const possibleMentorsForModule = (moduleName) => { return mentorList; }; // You can uncomment out this line to try your function -console.log(`${possibleMentorsForModule('using-apis')} can teach API module`); +// console.log(`${possibleMentorsForModule('using-apis')} can teach API module`); /** * Tjebbe wants to make it even easier for himself. @@ -25,6 +29,12 @@ console.log(`${possibleMentorsForModule('using-apis')} can teach API module`); * * It should return a single name. */ + +// const maxNum = 10; +// const minNum = 0; +// const randomNumber = Math.floor(Math.random() * (maxNum - minNum + 1) + minNum); +// console.log(randomNumber); + const findMentorForModule = (moduleName) => { const mentorList = []; let randomNum; @@ -33,8 +43,11 @@ const findMentorForModule = (moduleName) => { mentorList.push(mentor.name); } }); - randomNum = Math.floor(Math.random() * (mentorList.length - 1 - 0 + 1)) + 0; - return mentorList[randomNum]; + const maxNum = mentorList.length - 1; + const minNum = 0; + randomNum = Math.floor(Math.random() * (maxNum - minNum + 1) + minNum); + const randomMentor = mentorList[randomNum]; + return randomMentor; }; // You can uncomment out this line to try your function console.log( 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 2709c43..286d9d7 100644 --- a/Week3/prep-exercises/1-hyf-program/2-class-list.js +++ b/Week3/prep-exercises/1-hyf-program/2-class-list.js @@ -49,6 +49,49 @@ console.log(getPeopleOfClass('class34')); */ const getActiveClasses = () => { // TODO complete this function + + const activeClasses = classes + .map((eachClass) => { + if (eachClass.active) return eachClass.name; + }) + .filter((eachClass) => eachClass !== undefined); + + const activeStudent = students + .map((student) => { + if (student.graduated == false) + return { name: student.name, class: student.class, role: 'student' }; + }) + .filter((student) => student !== undefined); + + const activeModule = classes + .filter((eachClass) => eachClass.active) + .map((eachClass) => eachClass.currentModule); + + const activeMentor = mentors + .map((mentor) => { + if (activeModule.includes(mentor.nowTeaching)) { + const mentorName = mentor.name; + const className = classes + .filter((eachClass) => eachClass.currentModule == mentor.nowTeaching) + .map((eachClass) => eachClass.name) + .pop(); + return { name: mentorName, class: className, role: 'mentor' }; + } + }) + .filter((mentor) => mentor !== undefined); + + const activeClassObj = activeClasses.map((eachClass) => { + const studentList = activeStudent.filter( + (student) => student.class == eachClass + ); + const mentorList = activeMentor.filter( + (mentor) => mentor.class == eachClass + ); + return { [eachClass]: [...studentList, ...mentorList] }; + }); + + return JSON.stringify(activeClassObj, null, 2); }; // You can uncomment out this line to try your function -// console.log(getActiveClasses()); +console.log('active classes:'); +console.log(getActiveClasses()); From 11b76aa01929633f0b2af499767655b134401b87 Mon Sep 17 00:00:00 2001 From: Tenzin Kunchok <145247192+Tenzijn@users.noreply.github.com> Date: Sat, 18 Nov 2023 11:01:30 +0100 Subject: [PATCH 08/10] Update 1-find-mentors.js code improved --- Week3/prep-exercises/1-hyf-program/1-find-mentors.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) 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 c12e221..e25d686 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,10 @@ import { modules, students, mentors, classes } from './hyf.js'; * ['John', 'Mary'] */ -// then we take the name of the teacher and put it in a new array + const possibleMentorsForModule = (moduleName) => { - // first we take all the teacher who can teach the 'using apis' module + const mentorList = []; mentors.forEach((mentor) => { if (mentor.canTeach.includes(moduleName)) { @@ -21,7 +21,7 @@ const possibleMentorsForModule = (moduleName) => { return mentorList; }; // You can uncomment out this line to try your function -// console.log(`${possibleMentorsForModule('using-apis')} can teach API module`); + console.log(`${possibleMentorsForModule('using-apis')} can teach API module`); /** * Tjebbe wants to make it even easier for himself. From 90f1daaa055adb625df2689e21756feeaa294cc1 Mon Sep 17 00:00:00 2001 From: tenzijn Date: Fri, 24 Nov 2023 14:34:02 +0100 Subject: [PATCH 09/10] week4 prepexercise is done --- .../1-wallet/ex1-closure-example.js | 8 ++++---- Week4/prep-exercises/1-wallet/ex2-classes.js | 19 ++++++++++++++++++- Week4/prep-exercises/1-wallet/ex3-object.js | 17 +++++++++++++++++ .../1-wallet/ex4-object-shared-methods.js | 15 +++++++++++++++ .../prep-exercises/1-wallet/ex5-prototype.js | 13 +++++++++++++ 5 files changed, 67 insertions(+), 5 deletions(-) diff --git a/Week4/prep-exercises/1-wallet/ex1-closure-example.js b/Week4/prep-exercises/1-wallet/ex1-closure-example.js index e98b056..00b7b55 100644 --- a/Week4/prep-exercises/1-wallet/ex1-closure-example.js +++ b/Week4/prep-exercises/1-wallet/ex1-closure-example.js @@ -1,4 +1,4 @@ -import eurosFormatter from "./euroFormatter.js"; +import eurosFormatter from './euroFormatter.js'; /** * This is the closure way of doing things and we have already completed it for you so you don't need to do anything. @@ -68,9 +68,9 @@ function createWallet(name, cash = 0) { } function main() { - const walletJack = createWallet("Jack", 100); - const walletJoe = createWallet("Joe", 10); - const walletJane = createWallet("Jane", 20); + const walletJack = createWallet('Jack', 100); + const walletJoe = createWallet('Joe', 10); + const walletJane = createWallet('Jane', 20); walletJack.transferInto(walletJoe, 50); walletJack.setDailyAllowance(80); diff --git a/Week4/prep-exercises/1-wallet/ex2-classes.js b/Week4/prep-exercises/1-wallet/ex2-classes.js index f016137..8ee26b1 100644 --- a/Week4/prep-exercises/1-wallet/ex2-classes.js +++ b/Week4/prep-exercises/1-wallet/ex2-classes.js @@ -7,6 +7,7 @@ class Wallet { constructor(name, cash) { this.#name = name; this.#cash = cash; + this.dailyAllowance = 40; } get name() { @@ -23,6 +24,12 @@ class Wallet { return 0; } + // first check if the amount to withdraw is greater than the daily allowance + if (amount > this.dailyAllowance) { + console.log(`Insufficient remaining daily allowance!`); + return 0; + } + this.#cash -= amount; return amount; } @@ -33,10 +40,19 @@ class Wallet { wallet.name }` ); + const withdrawnAmount = this.withdraw(amount); wallet.deposit(withdrawnAmount); } + // setting daily allowance to 80 + setDailyAllowance(newAllowance) { + this.dailyAllowance = newAllowance; + console.log( + `Daily allowance set to: ${eurosFormatter.format(newAllowance)}` + ); + } + reportBalance() { console.log( `Name: ${this.name}, balance: ${eurosFormatter.format(this.#cash)}` @@ -49,9 +65,10 @@ function main() { const walletJoe = new Wallet('Joe', 10); const walletJane = new Wallet('Jane', 20); + walletJack.transferInto(walletJoe, 50); + walletJack.setDailyAllowance(80); walletJack.transferInto(walletJoe, 50); walletJane.transferInto(walletJoe, 25); - walletJane.deposit(20); walletJane.transferInto(walletJoe, 25); diff --git a/Week4/prep-exercises/1-wallet/ex3-object.js b/Week4/prep-exercises/1-wallet/ex3-object.js index e94faac..3865d01 100644 --- a/Week4/prep-exercises/1-wallet/ex3-object.js +++ b/Week4/prep-exercises/1-wallet/ex3-object.js @@ -4,6 +4,7 @@ function createWallet(name, cash = 0) { return { _name: name, _cash: cash, + _dailyAllowance: 40, // default value for daily allowance deposit: function (amount) { this._cash += amount; @@ -15,10 +16,24 @@ function createWallet(name, cash = 0) { return 0; } + // first check if the amount to withdraw is greater than the daily allowance + if (amount > this._dailyAllowance) { + console.log(`Insufficient remaining daily allowance!`); + return 0; + } + this._cash -= amount; return amount; }, + // setting daily allowance to 80 + setDailyAllowance: function (newAllowance) { + this._dailyAllowance = newAllowance; + console.log( + `Daily allowance set to: ${eurosFormatter.format(newAllowance)}` + ); + }, + transferInto: function (wallet, amount) { console.log( `Transferring ${eurosFormatter.format(amount)} from ${ @@ -46,6 +61,8 @@ function main() { const walletJoe = createWallet('Joe', 10); const walletJane = createWallet('Jane', 20); + walletJack.transferInto(walletJoe, 50); + walletJack.setDailyAllowance(80); walletJack.transferInto(walletJoe, 50); walletJane.transferInto(walletJoe, 25); 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..14eb1ce 100644 --- a/Week4/prep-exercises/1-wallet/ex4-object-shared-methods.js +++ b/Week4/prep-exercises/1-wallet/ex4-object-shared-methods.js @@ -10,10 +10,21 @@ function withdraw(amount) { return 0; } + // first check if the amount to withdraw is greater than the daily allowance + if (amount > this._dailyAllowance) { + console.log(`Insufficient remaining daily allowance!`); + return 0; + } + this._cash -= amount; return amount; } +function setDailyAllowance(newAllowance) { + this._dailyAllowance = newAllowance; + console.log(`Daily allowance set to: ${eurosFormatter.format(newAllowance)}`); +} + function transferInto(wallet, amount) { console.log( `Transferring ${eurosFormatter.format(amount)} from ${ @@ -38,11 +49,13 @@ function createWallet(name, cash = 0) { return { _name: name, _cash: cash, + _dailyAllowance: 40, // default value for daily allowance deposit, withdraw, transferInto, reportBalance, getName, + setDailyAllowance, }; } @@ -51,6 +64,8 @@ function main() { const walletJoe = createWallet('Joe', 10); const walletJane = createWallet('Jane', 20); + walletJack.transferInto(walletJoe, 50); + walletJack.setDailyAllowance(80); walletJack.transferInto(walletJoe, 50); walletJane.transferInto(walletJoe, 25); diff --git a/Week4/prep-exercises/1-wallet/ex5-prototype.js b/Week4/prep-exercises/1-wallet/ex5-prototype.js index 7cba410..121eccf 100644 --- a/Week4/prep-exercises/1-wallet/ex5-prototype.js +++ b/Week4/prep-exercises/1-wallet/ex5-prototype.js @@ -3,6 +3,7 @@ import eurosFormatter from './euroFormatter.js'; function Wallet(name, cash) { this._name = name; this._cash = cash; + this._dailyAllowance = 40; // default value for daily allowance } Wallet.prototype.deposit = function (amount) { @@ -14,11 +15,21 @@ Wallet.prototype.withdraw = function (amount) { console.log(`Insufficient funds!`); return 0; } + // first check if the amount to withdraw is greater than the daily allowance + if (amount > this._dailyAllowance) { + console.log(`Insufficient remaining daily allowance!`); + return 0; + } this._cash -= amount; return amount; }; +Wallet.prototype.setDailyAllowance = function (newAllowance) { + this._dailyAllowance = newAllowance; + console.log(`Daily allowance set to: ${eurosFormatter.format(newAllowance)}`); +}; + Wallet.prototype.transferInto = function (wallet, amount) { console.log( `Transferring ${eurosFormatter.format(amount)} from ${ @@ -44,6 +55,8 @@ function main() { const walletJoe = new Wallet('Joe', 10); const walletJane = new Wallet('Jane', 20); + walletJack.transferInto(walletJoe, 50); + walletJack.setDailyAllowance(80); walletJack.transferInto(walletJoe, 50); walletJane.transferInto(walletJoe, 25); From a53e2a1b042bbe191602c70bc7c6079e6ed9ed3f Mon Sep 17 00:00:00 2001 From: tenzijn Date: Fri, 24 Nov 2023 15:29:23 +0100 Subject: [PATCH 10/10] vscode and json file updated --- .vscode/settings.json | 3 +++ package.json | 13 +++++++++++++ 2 files changed, 16 insertions(+) create mode 100644 .vscode/settings.json create mode 100644 package.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..febb024 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "githubPullRequests.ignoredPullRequestBranches": ["main"] +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..2810091 --- /dev/null +++ b/package.json @@ -0,0 +1,13 @@ +{ + "name": "javascript", + "version": "1.0.0", + "description": "> If you are following the HackYourFuture curriculum we recommend you to start with module 1: [HTML/CSS/GIT](https://github.com/HackYourFuture/HTML-CSS). To get a complete overview of the HackYourFuture curriculum first, click [here](https://github.com/HackYourFuture/curriculum).", + "main": "index.js", + "type": "module", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [], + "author": "", + "license": "ISC" +}