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/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!`);
+}
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..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,34 @@
-"use strict";
+'use strict';
/**
* The `state` property says what the traffic light's state (i.e. colour) is at
* that moment.
*/
-const trafficLight = {
- state: "green",
+let trafficLight = {
+ 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"){
+ trafficLight.state = "red";
+}else{
+ rotations++;
+ trafficLight.state = "green";
+}
+*/
// TODO
// if the color is green, turn it orange
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..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,18 +1,31 @@
-"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++;
+ }
+ */
// 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..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 @@
+
+
+
+
+
+ Dice experimentation
+
+
+ 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
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..e25d686 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.
@@ -7,11 +7,21 @@ import { modules, students, mentors, classes } from "./hyf.js";
* It should return an array of names. So something like:
* ['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.
@@ -19,8 +29,27 @@ const possibleMentorsForModule = (moduleName) => {
*
* 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) => {
- // TODO complete this function
+ const mentorList = [];
+ let randomNum;
+ mentors.forEach((mentor) => {
+ if (mentor.canTeach.includes(moduleName)) {
+ mentorList.push(mentor.name);
+ }
+ });
+ 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(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..286d9d7 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.
@@ -31,6 +49,49 @@ const getPeopleOfClass = (className) => {
*/
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());
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);
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"
+}