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..f50aa6b 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); + //const currentState = trafficLight.state; + + if (trafficLight.state === "green") { + trafficLight.state = "orange"; + } else if (trafficLight.state === "orange") { + trafficLight.state = "red"; + } else if (trafficLight.state === "red") { + trafficLight.state = "green"; + + rotations++; + } + console.log("The traffic light is on", trafficLight.state); + // 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..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,6 +14,16 @@ while (cycle < 2) { const currentState = trafficLight.possibleStates[trafficLight.stateIndex]; console.log("The traffic light is on", currentState); + 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++; + } + // TODO // if the color is green, turn it orange // if the color is orange, turn it red 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..a0b1eeb 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: @@ -14,6 +20,14 @@ function runExperiment(sampleSize) { // element for value 2, etc. const results = []; + let count = 0; + + 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 @@ -29,7 +43,11 @@ 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); + } // TODO // Write a for..of loop that calls the `runExperiment()` function for each 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/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/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..91b3cba 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,57 @@ import { modules, students, mentors, classes } from "./hyf.js"; * * [{ name: 'John', role: 'student' }, { name: 'Mary', role: 'mentor' }] */ + + + +//console.log(getModuleName('class34')); + const getPeopleOfClass = (className) => { - // TODO complete 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 false; + } + + // 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)) { + let mentor = { + name: mentors[i].name, + role: 'mentor' + }; + + activePeople.push(mentor); + } + } + + // Searching students who study in a class + for (let i = 0; i { * } */ const getActiveClasses = () => { - // TODO complete this function + let activeClasses = {}; + + for (let i = 0; i < classes.length; i++) { + if (classes[i].active) { + activeClasses[classes[i].name] = getPeopleOfClass(classes[i].name); + } + } + + return activeClasses; }; -// You can uncomment out this line to try your function -// console.log(getActiveClasses()); + +console.log(getActiveClasses()); diff --git a/Week4/prep-exercises/1-wallet/ex2-classes.js b/Week4/prep-exercises/1-wallet/ex2-classes.js index f016137..4f64262 100644 --- a/Week4/prep-exercises/1-wallet/ex2-classes.js +++ b/Week4/prep-exercises/1-wallet/ex2-classes.js @@ -3,10 +3,14 @@ import eurosFormatter from './euroFormatter.js'; class Wallet { #name; #cash; + #dailyAllowance; + #dayTotalWithdrawals; - constructor(name, cash) { + constructor(name, cash, dailyAllowance = 40) { this.#name = name; this.#cash = cash; + this.#dailyAllowance = dailyAllowance; + this.#dayTotalWithdrawals = 0; } get name() { @@ -19,11 +23,17 @@ class Wallet { withdraw(amount) { if (this.#cash - amount < 0) { - console.log(`Insufficient funds!`); + 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; } @@ -42,6 +52,14 @@ class Wallet { `Name: ${this.name}, balance: ${eurosFormatter.format(this.#cash)}` ); } + + resetDailyAllowance() { + this.#dayTotalWithdrawals = 0; + } + + setDailyAllowance(newAllowance) { + this.#dailyAllowance = newAllowance; + } } function main() { @@ -60,4 +78,4 @@ function main() { walletJane.reportBalance(); } -main(); +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..5b80b72 100644 --- a/Week4/prep-exercises/1-wallet/ex3-object.js +++ b/Week4/prep-exercises/1-wallet/ex3-object.js @@ -4,6 +4,8 @@ function createWallet(name, cash = 0) { return { _name: name, _cash: cash, + _dailyAllowance: 40, + _dayTotalWithdrawals: 0, deposit: function (amount) { this._cash += amount; @@ -11,11 +13,17 @@ function createWallet(name, cash = 0) { withdraw: function (amount) { if (this._cash - amount < 0) { - console.log(`Insufficient funds!`); + 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; }, @@ -38,6 +46,14 @@ function createWallet(name, cash = 0) { getName: function () { return this._name; }, + + resetDailyAllowance: function () { + this._dayTotalWithdrawals = 0; + }, + + setDailyAllowance: function (newAllowance) { + this._dailyAllowance = newAllowance; + } }; } @@ -57,4 +73,4 @@ function main() { walletJane.reportBalance(); } -main(); +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..87f6756 100644 --- a/Week4/prep-exercises/1-wallet/ex4-object-shared-methods.js +++ b/Week4/prep-exercises/1-wallet/ex4-object-shared-methods.js @@ -6,11 +6,17 @@ function deposit(amount) { function withdraw(amount) { if (this._cash - amount < 0) { - console.log(`Insufficient funds!`); + 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; } @@ -30,6 +36,14 @@ function reportBalance() { ); } +function resetDailyAllowance() { + this._dayTotalWithdrawals = 0; +} + +function setDailyAllowance(newAllowance) { + this._dailyAllowance = newAllowance; +} + function getName() { return this._name; } @@ -38,10 +52,14 @@ function createWallet(name, cash = 0) { return { _name: name, _cash: cash, + _dailyAllowance: 40, + _dayTotalWithdrawals: 0, deposit, withdraw, transferInto, reportBalance, + resetDailyAllowance, + setDailyAllowance, getName, }; } @@ -62,4 +80,4 @@ function main() { walletJane.reportBalance(); } -main(); +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..f8036f2 100644 --- a/Week4/prep-exercises/1-wallet/ex5-prototype.js +++ b/Week4/prep-exercises/1-wallet/ex5-prototype.js @@ -3,6 +3,8 @@ import eurosFormatter from './euroFormatter.js'; function Wallet(name, cash) { this._name = name; this._cash = cash; + this._dailyAllowance = 40; + this._dayTotalWithdrawals = 0; } Wallet.prototype.deposit = function (amount) { @@ -15,7 +17,13 @@ Wallet.prototype.withdraw = function (amount) { return 0; } + if (this._dayTotalWithdrawals + amount > this._dailyAllowance) { + console.log(`Insufficient remaining daily allowance!`); + return 0; + } + this._cash -= amount; + this._dayTotalWithdrawals += amount; return amount; }; @@ -26,7 +34,9 @@ Wallet.prototype.transferInto = function (wallet, amount) { } to ${wallet.getName()}` ); const withdrawnAmount = this.withdraw(amount); - wallet.deposit(withdrawnAmount); + if (withdrawnAmount > 0) { + wallet.deposit(withdrawnAmount); + } }; Wallet.prototype.reportBalance = function () { @@ -39,6 +49,14 @@ 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); @@ -55,4 +73,4 @@ function main() { walletJane.reportBalance(); } -main(); +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" +}