From 18dbfc5cfc98c2de990841bd6c0c9e813379f438 Mon Sep 17 00:00:00 2001 From: Esen Karatas Date: Fri, 20 Sep 2024 22:39:25 +0200 Subject: [PATCH 1/6] [Feature] Updated week1 exercises after fork. --- .../1-traffic-light/traffic-light-1.js | 30 +++++---------- .../1-traffic-light/traffic-light-2.js | 38 ++++++------------- 2 files changed, 22 insertions(+), 46 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..75ccd45 100644 --- a/Week1/prep-exercises/1-traffic-light/traffic-light-1.js +++ b/Week1/prep-exercises/1-traffic-light/traffic-light-1.js @@ -1,8 +1,5 @@ "use strict"; -/** - * The `state` property says what the traffic light's state (i.e. colour) is at - * that moment. - */ + const trafficLight = { state: "green", }; @@ -12,20 +9,13 @@ while (rotations < 2) { const currentState = trafficLight.state; console.log("The traffic light is on", currentState); - // TODO - // if the color is green, turn it orange - // if the color is orange, turn it red - // if the color is red, add 1 to rotations and turn it green + if (currentState === 'green') { + trafficLight.state = 'orange'; + } else if (currentState === 'orange') { + trafficLight.state = 'red'; + } + else if (currentState === 'red') { + trafficLight.state = 'green'; + rotations++ + } } - -/** - * The output should be: - -The traffic light is on green -The traffic light is on orange -The traffic light is on red -The traffic light is on green -The traffic light is on orange -The traffic light is on red - -*/ 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..e5f9b5f 100644 --- a/Week1/prep-exercises/1-traffic-light/traffic-light-2.js +++ b/Week1/prep-exercises/1-traffic-light/traffic-light-2.js @@ -1,33 +1,19 @@ "use strict"; -/** - * The `possibleStates` property define the states (in this case: colours) - * in which the traffic light can be. - * The `stateIndex` property indicates which of the possible states is current. - */ + const trafficLight = { - possibleStates: ["green", "orange", "red"], - stateIndex: 0, + 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); - - // TODO - // if the color is green, turn it orange - // if the color is orange, turn it red - // if the color is red, add 1 to cycles and turn it green -} - -/** - * The output should be: - -The traffic light is on green -The traffic light is on orange -The traffic light is on red -The traffic light is on green -The traffic light is on orange -The traffic light is on red + const currentState = trafficLight.possibleStates[trafficLight.stateIndex]; + console.log("The traffic light is on", currentState); -*/ + if (currentState === 'red') { + cycle++; + trafficLight.stateIndex = 0; + } else { + trafficLight.stateIndex++; + } +} \ No newline at end of file From 864a0025a7261974e226868d8905c8a5241ea691 Mon Sep 17 00:00:00 2001 From: Esen Karatas Date: Fri, 20 Sep 2024 23:56:27 +0200 Subject: [PATCH 2/6] [Feature] Added functionality to the getCurrentState and getStateIndex functions --- .../1-traffic-light/traffic-light.js | 20 +++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/Week2/prep-exercises/1-traffic-light/traffic-light.js b/Week2/prep-exercises/1-traffic-light/traffic-light.js index f4a5c1a..8862e31 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. + + const index = trafficLight.stateIndex; + return trafficLight.possibleStates[index]; + } function getNextStateIndex(trafficLight) { - // TODO - // Return the index of the next state of the `trafficLight` such that: - // - if the color is green, it will turn to orange - // - if the color is orange, it will turn to red - // - if the color is red, it will turn to green + const index = trafficLight.stateIndex; + if (index === 2) { + return 0 + + } else { + return trafficLight.stateIndex += 1 + } + } // This function loops for the number of seconds specified by the `secs` From ce29ec0aa16d21117c673624f47a0e0e3d949a9d Mon Sep 17 00:00:00 2001 From: Esen Karatas Date: Sat, 21 Sep 2024 23:05:54 +0200 Subject: [PATCH 3/6] [Feature] Added dice functionality to the run experiment function. --- Week2/prep-exercises/2-experiments/index.js | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/Week2/prep-exercises/2-experiments/index.js b/Week2/prep-exercises/2-experiments/index.js index 7e5aa92..37cab8f 100644 --- a/Week2/prep-exercises/2-experiments/index.js +++ b/Week2/prep-exercises/2-experiments/index.js @@ -2,25 +2,33 @@ function runExperiment(sampleSize) { const valueCounts = [0, 0, 0, 0, 0, 0]; + for (let i = 0; i < sampleSize; i++) { + const randomNumber = Math.floor(Math.random() * 6); + valueCounts[randomNumber] += 1; + } // TODO // Write a for loop that iterates `sampleSize` times (sampleSize is a number). // In each loop iteration: // - // 1. Generate a random integer between 1 and 6 (as if throwing a six-sided die). + // 1. Generate a random integer between 1 and 6 (as if throwing a six-sided dice). // 2. Add `1` to the element of the `valueCount` that corresponds to the random // value from the previous step. Use the first element of `valueCounts` // for keeping a count how many times the value 1 is thrown, the second // element for value 2, etc. const results = []; + for (const element of valueCounts) { + const percentage = 100 * element / sampleSize; + results.push(percentage.toFixed(2)); + } // TODO // Write a for..of loop for the `valueCounts` array created in the previous // loop. In each loop iteration: - // 1. For each possible value of the die (1-6), compute the percentage of how + // 1. For each possible value of the dice (1-6), compute the percentage of how // many times that value was thrown. Remember that the first value of - // `valueCounts` represent the die value of 1, etc. + // `valueCounts` represent the dice value of 1, etc. // 2. Convert the computed percentage to a number string with a precision of // two decimals, e.g. '14.60'. // 3. Then push that string onto the `results` array. @@ -30,6 +38,10 @@ function runExperiment(sampleSize) { function main() { const sampleSizes = [100, 1000, 1000000]; + for (const element of sampleSizes) { + console.log(runExperiment(element), element); + + } // TODO // Write a for..of loop that calls the `runExperiment()` function for each From 1f4b04b17d96401df4ffbc3a57f5946bc61228a4 Mon Sep 17 00:00:00 2001 From: Esen Karatas Date: Sat, 21 Sep 2024 23:09:37 +0200 Subject: [PATCH 4/6] [Refactor] Removed code instructions. --- Week2/prep-exercises/2-experiments/index.js | 30 --------------------- 1 file changed, 30 deletions(-) diff --git a/Week2/prep-exercises/2-experiments/index.js b/Week2/prep-exercises/2-experiments/index.js index 37cab8f..e433654 100644 --- a/Week2/prep-exercises/2-experiments/index.js +++ b/Week2/prep-exercises/2-experiments/index.js @@ -7,32 +7,12 @@ function runExperiment(sampleSize) { valueCounts[randomNumber] += 1; } - // TODO - // Write a for loop that iterates `sampleSize` times (sampleSize is a number). - // In each loop iteration: - // - // 1. Generate a random integer between 1 and 6 (as if throwing a six-sided dice). - // 2. Add `1` to the element of the `valueCount` that corresponds to the random - // value from the previous step. Use the first element of `valueCounts` - // for keeping a count how many times the value 1 is thrown, the second - // element for value 2, etc. - const results = []; for (const element of valueCounts) { const percentage = 100 * element / sampleSize; results.push(percentage.toFixed(2)); } - // TODO - // Write a for..of loop for the `valueCounts` array created in the previous - // loop. In each loop iteration: - // 1. For each possible value of the dice (1-6), compute the percentage of how - // many times that value was thrown. Remember that the first value of - // `valueCounts` represent the dice value of 1, etc. - // 2. Convert the computed percentage to a number string with a precision of - // two decimals, e.g. '14.60'. - // 3. Then push that string onto the `results` array. - return results; } @@ -43,16 +23,6 @@ function main() { } - // 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. - // 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 } main(); From c50b36e55d0dfaca8c5336cd53b35d8fe03d30eb Mon Sep 17 00:00:00 2001 From: Esen Karatas Date: Thu, 26 Sep 2024 22:06:49 +0200 Subject: [PATCH 5/6] [Feature] Added functions to find possible mentors for a module and select a random mentor --- Week3/prep-exercises/1-hyf-program/1-find-mentors.js | 12 ++++++++---- 1 file changed, 8 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 72baa61..38ca94d 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,11 @@ import { modules, students, mentors, classes } from "./hyf.js"; * ['John', 'Mary'] */ const possibleMentorsForModule = (moduleName) => { - // TODO complete this function + const possibleMentors = mentors.filter((mentor) => mentor.canTeach.includes(moduleName)) + return possibleMentors; }; // 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 +21,10 @@ const possibleMentorsForModule = (moduleName) => { * It should return a single name. */ const findMentorForModule = (moduleName) => { - // TODO complete this function + const randomNumber = Math.floor(Math.random()); + const filteredMentors = possibleMentorsForModule(moduleName); + const randomMentor = filteredMentors[randomNumber].name; + return randomMentor; }; // You can uncomment out this line to try your function -// console.log(findMentorForModule('javascript')); +console.log(findMentorForModule('javascript')); From 5df2f6616592a2990e0761e2a487b8407ce6dcc1 Mon Sep 17 00:00:00 2001 From: Esen Karatas Date: Sat, 28 Sep 2024 11:52:00 +0200 Subject: [PATCH 6/6] [Feature] Added functions to get class participants and active classes --- .vscode/launch.json | 7 ++++ .../1-hyf-program/1-find-mentors.js | 16 +--------- .../1-hyf-program/2-class-list.js | 32 +++++++++++++++---- 3 files changed, 34 insertions(+), 21 deletions(-) create mode 100644 .vscode/launch.json diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..5c7247b --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,7 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [] +} \ No newline at end of file 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 38ca94d..9be1114 100644 --- a/Week3/prep-exercises/1-hyf-program/1-find-mentors.js +++ b/Week3/prep-exercises/1-hyf-program/1-find-mentors.js @@ -1,30 +1,16 @@ import { modules, students, mentors, classes } from "./hyf.js"; -/** - * Tjebbe would like help to get a list of possible mentors for a module. - * Fill in this function that finds all the mentors that can teach the given module. - * - * It should return an array of names. So something like: - * ['John', 'Mary'] - */ const possibleMentorsForModule = (moduleName) => { const possibleMentors = mentors.filter((mentor) => mentor.canTeach.includes(moduleName)) return possibleMentors; }; -// You can uncomment out this line to try your function + console.log(possibleMentorsForModule('using-apis')); -/** - * Tjebbe wants to make it even easier for himself. - * Fill in this function that chooses a random mentor to teach the given module. - * - * It should return a single name. - */ const findMentorForModule = (moduleName) => { const randomNumber = Math.floor(Math.random()); const filteredMentors = possibleMentorsForModule(moduleName); const randomMentor = filteredMentors[randomNumber].name; return randomMentor; }; -// You can uncomment out this line to try your function console.log(findMentorForModule('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..09c7dbf 100644 --- a/Week3/prep-exercises/1-hyf-program/2-class-list.js +++ b/Week3/prep-exercises/1-hyf-program/2-class-list.js @@ -12,10 +12,23 @@ import { modules, students, mentors, classes } from "./hyf.js"; * [{ name: 'John', role: 'student' }, { name: 'Mary', role: 'mentor' }] */ const getPeopleOfClass = (className) => { - // TODO complete this function + const studentsOfClass = students + .filter((student) => student.class === className) + .map((student) => ({ name: student.name, role: "student" })); + const classroom = classes.find((classroom) => classroom.name === className); + + const mentor = mentors + .filter( + (mentor) => + mentor.nowTeaching === classroom.currentModule && + classroom.currentModule + ) + .map((mentor) => ({ name: mentor.name, role: "mentor" })); + const entireClass = [...studentsOfClass, ...mentor]; + return entireClass; }; -// 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. @@ -30,7 +43,14 @@ const getPeopleOfClass = (className) => { * } */ const getActiveClasses = () => { - // TODO complete this function + const result = {}; + classes + .filter((classroom) => (classroom.active === true)) + .forEach((classroom) => { + result[classroom.name] = getPeopleOfClass(classroom.name); + }); + + return result; }; -// You can uncomment out this line to try your function -// console.log(getActiveClasses()); + + console.log(getActiveClasses());