From c06454682f6efe1c35ceacc27b9ebaadbfe49a9e Mon Sep 17 00:00:00 2001 From: Samira Date: Mon, 25 Nov 2024 10:07:03 +0100 Subject: [PATCH 1/2] update change --- .../1-traffic-light/traffic-light-1.js | 19 +++-------- .../1-traffic-light/traffic-light-2.js | 32 +++++++------------ 2 files changed, 16 insertions(+), 35 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..982af8a 100644 --- a/Week1/prep-exercises/1-traffic-light/traffic-light-1.js +++ b/Week1/prep-exercises/1-traffic-light/traffic-light-1.js @@ -11,21 +11,12 @@ let rotations = 0; 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 } -/** - * The output should be: +if (currentState === "green") { trafficLight.state = "orange"; -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 +} else if (currentState === "orange") { trafficLight.state = "red"; -*/ + } else if (currentState === "red") { trafficLight.state = "green"; + rotations++; + } \ No newline at end of file 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..5429fbf 100644 --- a/Week1/prep-exercises/1-traffic-light/traffic-light-2.js +++ b/Week1/prep-exercises/1-traffic-light/traffic-light-2.js @@ -1,9 +1,5 @@ "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, @@ -14,20 +10,14 @@ 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 + if (currentState === "green") + { trafficLight.stateIndex = 1; + } else if (currentState === "orange") + { trafficLight.stateIndex = 2 ; -*/ + } else if (currentState === "red") { + trafficLight.stateIndex = 0; + + cycle++; + } + } \ No newline at end of file From aede6164036e48a7df412d6e019b558e68401974 Mon Sep 17 00:00:00 2001 From: samira Date: Tue, 3 Dec 2024 16:04:36 +0100 Subject: [PATCH 2/2] Added solutions for week 3 exercises --- Week3/challenges/1-sum-entries.js | 9 ++++++- Week3/challenges/2-sum-three-entries.js | 11 +++++++- Week3/challenges/3-password-validation.js | 9 ++++++- Week3/challenges/4-bank-account.js | 26 +++++++++++++++++-- .../1-hyf-program/1-find-mentors.js | 13 ++++++++-- .../1-hyf-program/2-class-list.js | 21 +++++++++++++-- 6 files changed, 80 insertions(+), 9 deletions(-) diff --git a/Week3/challenges/1-sum-entries.js b/Week3/challenges/1-sum-entries.js index f7dd419..db73b4f 100644 --- a/Week3/challenges/1-sum-entries.js +++ b/Week3/challenges/1-sum-entries.js @@ -10,7 +10,14 @@ 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 = i + 1; j < list.length; j++ ) { + if (list[i] + list[j] === 2020 ){ + result = list[i] * list[j]; + break; + } + } +} // 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..3553415 100644 --- a/Week3/challenges/2-sum-three-entries.js +++ b/Week3/challenges/2-sum-three-entries.js @@ -10,7 +10,16 @@ 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 = i + 1; j < list.length; j++ ) { + for ( let k = j + 1; k < list.length; k++ ) { + if (list[i] + list[j] + list[k] === 2020 ){ + result = list[i] * list[j] * list[k]; + break; + } + } + } +} // TEST CODE, do not change diff --git a/Week3/challenges/3-password-validation.js b/Week3/challenges/3-password-validation.js index fa7ad11..80e5e5b 100644 --- a/Week3/challenges/3-password-validation.js +++ b/Week3/challenges/3-password-validation.js @@ -20,4 +20,11 @@ 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 +]; +passwordList.forEach(item => { + const [ min, max] = item.times.split("-").map(Number); + const leetterCount = item.password.split("").filter(char => char === item.letter).length; + const isValid = leetterCount >= min && leetterCount <= max; + + console.log(`Password '${item.password}' is ${isValid? "VALID" : "INVALID"}`); +}) \ No newline at end of file diff --git a/Week3/challenges/4-bank-account.js b/Week3/challenges/4-bank-account.js index 8f0f035..78790ed 100644 --- a/Week3/challenges/4-bank-account.js +++ b/Week3/challenges/4-bank-account.js @@ -28,10 +28,32 @@ const bankAccount = { }; const donateMoney = (amount, onSuccess, onFail) => { - // TODO complete this function + if (bankAccount.currentBalance >= amount) { + bankAccount.currentBalance -= amount; + + bankAccount.transactions.push({ + prevAmount: bankAccount.currentBalance + amount, + newAmount: bankAccount.currentBalance, + reason: "Donation", + }); + onSuccess(); + }else { + onFail(); + } }; const payRent = (amount, onSuccess, onFail) => { - // TODO complete this function + if (bankAccount.currentBalance >= amount) { + bankAccount.currentBalance -= amount; + + bankAccount.transactions.push({ + prevAmount: bankAccount.currentBalance + amount, + newAmount: bankAccount.currentBalance, + reason: "Rent", + }); + onSuccess(); + } else { + onFail(); + } }; /** 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..9785b02 100644 --- a/Week3/prep-exercises/1-hyf-program/1-find-mentors.js +++ b/Week3/prep-exercises/1-hyf-program/1-find-mentors.js @@ -8,7 +8,9 @@ import { modules, students, mentors, classes } from "./hyf.js"; * ['John', 'Mary'] */ const possibleMentorsForModule = (moduleName) => { - // TODO complete this function + const mentorsTeachingModule = mentors.filter((mentor) => mentor.canTeach.includes(moduleName)); + + return mentorsTeachingModule.map((mentor) => mentor.name); }; // You can uncomment out this line to try your function // console.log(possibleMentorsForModule('using-apis')); @@ -20,7 +22,14 @@ const possibleMentorsForModule = (moduleName) => { * It should return a single name. */ const findMentorForModule = (moduleName) => { - // TODO complete this function + const possibleMentors = possibleMentorsForModule(moduleName); + + if (!possibleMentors.length) { + console.error(`No mentors found for module ${moduleName}`); + return; + } + + return possibleMentors[Math.floor(Math.random() * possibleMentors.length)]; }; // 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..e2d82ca 100644 --- a/Week3/prep-exercises/1-hyf-program/2-class-list.js +++ b/Week3/prep-exercises/1-hyf-program/2-class-list.js @@ -12,7 +12,18 @@ import { modules, students, mentors, classes } from "./hyf.js"; * [{ name: 'John', role: 'student' }, { name: 'Mary', role: 'mentor' }] */ const getPeopleOfClass = (className) => { - // TODO complete this function + const classInfo = classes.find(cls => cls.name === className); + if (!classInfo) { + console.error(`Class ${className} not found.`); + return []; + } + const studentsOfClass = students.filter(students => students.class === className) + .map(students => ({ name: students.name, role:'student' })); + + const mentorsOfClass = mentors.filter(mentors => mentors.nowTeaching === classInfo.currentModule) + .map(mentors => ({ name: mentors.name, role:'mentor' })); + + return [...studentsOfClass,...mentorsOfClass]; }; // You can uncomment out this line to try your function // console.log(getPeopleOfClass('class34')); @@ -30,7 +41,13 @@ const getPeopleOfClass = (className) => { * } */ const getActiveClasses = () => { - // TODO complete this function + const activeClasses = classes.filter(cls => cls.active); + + const result = {}; + activeClasses.forEach(activeClasses => { + result[activeClasses.name] = getPeopleOfClass(activeClasses.name); + }); + return result; }; // You can uncomment out this line to try your function // console.log(getActiveClasses());