Skip to content

Samira w3 exercises #61

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 5 additions & 14 deletions Week1/prep-exercises/1-traffic-light/traffic-light-1.js
Original file line number Diff line number Diff line change
Expand Up @@ -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++;
}
32 changes: 11 additions & 21 deletions Week1/prep-exercises/1-traffic-light/traffic-light-2.js
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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++;
}
}
9 changes: 8 additions & 1 deletion Week3/challenges/1-sum-entries.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 10 additions & 1 deletion Week3/challenges/2-sum-three-entries.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 8 additions & 1 deletion Week3/challenges/3-password-validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'}
];
];
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"}`);
})
26 changes: 24 additions & 2 deletions Week3/challenges/4-bank-account.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
};

/**
Expand Down
13 changes: 11 additions & 2 deletions Week3/prep-exercises/1-hyf-program/1-find-mentors.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand All @@ -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'));
21 changes: 19 additions & 2 deletions Week3/prep-exercises/1-hyf-program/2-class-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand All @@ -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());