Skip to content
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

modified traffic-light.js #60

Open
wants to merge 8 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

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 5 additions & 10 deletions Week2/prep-exercises/1-traffic-light/traffic-light.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,12 @@
*/

function getCurrentState(trafficLight) {
// TODO
// Should return the current state (i.e. colour) of the `trafficLight`
// object passed as a parameter.
}

return trafficLight.possibleStates[trafficLight.stateIndex];}

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
function getNextStateIndex(trafficLight)
{
return (trafficLight.stateIndex + 1) % trafficLight.possibleStates.length;
}

// This function loops for the number of seconds specified by the `secs`
Expand Down
11 changes: 7 additions & 4 deletions Week3/prep-exercises/1-hyf-program/1-find-mentors.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ 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 possibleMentorsForModule = (moduleName) => mentors.filter(mentor => mentor.modules.includes(moduleName)).map(mentor => mentor.name);


// You can uncomment out this line to try your function
// console.log(possibleMentorsForModule('using-apis'));

Expand All @@ -20,7 +20,10 @@ const possibleMentorsForModule = (moduleName) => {
* It should return a single name.
*/
const findMentorForModule = (moduleName) => {
// TODO complete this function
const possibleMentors = possibleMentorsForModule(moduleName);
if (possibleMentors.length === 0) return null;
const randomIndex = Math.floor(Math.random() * possibleMentors.length);
return possibleMentors[randomIndex];
};
// You can uncomment out this line to try your function
// console.log(findMentorForModule('javascript'));
27 changes: 24 additions & 3 deletions Week3/prep-exercises/1-hyf-program/2-class-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,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 getPeopleOfClass = (className) =>
{
const classObj = classes.find(c => c.name === className);
if (!classObj) {
console.error(`Class "${className}" not found.`);
return [];
};

const currentModule = classObj.currentModule;
const teachingMentors = mentors
.filter(mentor => mentor.nowTeaching === currentModule)
.map(mentor => ({ name: mentor.name, role: 'mentor' }));
const classStudents = classObj.students.map(student => ({ name: student, role: 'student' }));

return [...classStudents, ...teachingMentors];
};

// You can uncomment out this line to try your function
// console.log(getPeopleOfClass('class34'));

Expand All @@ -30,7 +44,14 @@ const getPeopleOfClass = (className) => {
* }
*/
const getActiveClasses = () => {
// TODO complete this function
const activeClasses = classes.filter(c => c.active);
const result = {};
activeClasses.forEach(classObj => {
result[classObj.name] = getPeopleOfClass(classObj.name);
});

return result;

};
// You can uncomment out this line to try your function
// console.log(getActiveClasses());
15 changes: 3 additions & 12 deletions Week4/prep-exercises/1-wallet/ex1-closure-example.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,32 +29,23 @@ function createWallet(name, cash = 0) {
return amount;
}

function transferInto(wallet, amount) {
console.log(
`Transferring ${eurosFormatter.format(
amount
)} from ${name} to ${wallet.getName()}`
);
const withdrawnAmount = withdraw(amount);
wallet.deposit(withdrawnAmount);
function resetDailyAllowance() {
dayTotalWithdrawals = 0;
}


function setDailyAllowance(newAllowance) {
dailyAllowance = newAllowance;
console.log(
`Daily allowance set to: ${eurosFormatter.format(newAllowance)}`
);
}

function resetDailyAllowance() {
dayTotalWithdrawals = 0;
}

function reportBalance() {
console.log(`Name: ${name}, balance: ${eurosFormatter.format(cash)}`);
}

const getName = () => name;

return {
deposit,
Expand Down
37 changes: 21 additions & 16 deletions Week4/prep-exercises/1-wallet/ex2-classes.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,47 @@
import eurosFormatter from './euroFormatter.js';

class Wallet {
#name;
#cash;

constructor(name, cash) {
this.#name = name;
this.#cash = cash;
}

get name() {
return this.#name;
this.dailyALLowance = 40;
this.dayTotalWithdrawals = 0;
}

deposit(amount) {
this.#cash += amount;
this._cash += amount;
}

withdraw(amount) {
if (this.#cash - amount < 0) {
if (this._cash - amount < 0) {
console.log(`Insufficient funds!`);
return 0;
}

this.#cash -= amount;
if (this.dayTotalWithdrawals + amount > this.dailyAllowance) {
console.log(`Insufficient remaining daily allowance!`);
return 0;
}

this._cash -= amount;
this.dayTotalWithdrawals += amount;
return amount;
}

transferInto(wallet, amount) {
console.log(
`Transferring ${eurosFormatter.format(amount)} from ${this.name} to ${
wallet.name
}`
);
transferInto(wallet, amount) {
const withdrawnAmount = this.withdraw(amount);
wallet.deposit(withdrawnAmount);
}

resetDailyAllowance() {
this.dayTotalWithdrawals = 0;
}

setDailyAllowance(newAllowance) {
this.dailyAllowance = newAllowance;
console.log(`Daily allowance set to: ${newAllowance}`);
}

reportBalance() {
console.log(
`Name: ${this.name}, balance: ${eurosFormatter.format(this.#cash)}`
Expand Down
26 changes: 20 additions & 6 deletions Week4/prep-exercises/1-wallet/ex3-object.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import eurosFormatter from './euroFormatter.js';

function createWallet(name, cash = 0) {
let dailyAllowance = 40;
let dayTotalWithdrawals = 0;
return {
_name: name,
_cash: cash,
dailyAllowance,
dayTotalWithdrawals,

deposit: function (amount) {


deposit(amount) {
this._cash += amount;
},

Expand All @@ -16,18 +22,26 @@ function createWallet(name, cash = 0) {
}

this._cash -= amount;
this.dayTotalWithdrawals += amount;
return amount;
},

transferInto: function (wallet, amount) {
console.log(
`Transferring ${eurosFormatter.format(amount)} from ${
this._name
} to ${wallet.getName()}`
);

const withdrawnAmount = this.withdraw(amount);
wallet.deposit(withdrawnAmount);
},
resetDailyAllowance() {
this.dayTotalWithdrawals = 0;
},

setDailyAllowance(newAllowance) {
this.dailyAllowance = newAllowance;
console.log(`Daily allowance set to: ${newAllowance}`);
},




reportBalance: function () {
console.log(
Expand Down
29 changes: 27 additions & 2 deletions Week4/prep-exercises/1-wallet/ex4-object-shared-methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,18 @@ function withdraw(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;
}
};



function transferInto(wallet, amount) {
console.log(
Expand All @@ -28,16 +37,32 @@ function reportBalance() {
console.log(
`Name: ${this._name}, balance: ${eurosFormatter.format(this._cash)}`
);
}
};

function getName() {
return this._name;
};

function resetDailyAllowance() {
this._dayTotalWithdrawals = 0;
console.log(`${this._name}'s daily withdrawals have been reset.`);
}

function setDailyAllowance(newAllowance) {
this._dailyAllowance = newAllowance;
console.log(
`${this._name}'s daily allowance set to: ${eurosFormatter.format(
newAllowance
)}`
);
}

function createWallet(name, cash = 0) {
return {
_name: name,
_cash: cash,
_dailyAllowance: 40,
_dayTotalWithdrawals: 0,
deposit,
withdraw,
transferInto,
Expand Down
24 changes: 18 additions & 6 deletions Week4/prep-exercises/1-wallet/ex5-prototype.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import eurosFormatter from './euroFormatter.js';

function Wallet(name, cash) {
function Wallet(name, cash = 0 ) {
this._name = name;
this._cash = cash;
this.dailyAllowance = 40;
this.dayTotalWithdrawals = 0;
}

Wallet.prototype.deposit = function (amount) {
Expand All @@ -15,20 +17,30 @@ 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;
};

Wallet.prototype.transferInto = function (wallet, amount) {
console.log(
`Transferring ${eurosFormatter.format(amount)} from ${
this._name
} to ${wallet.getName()}`
);
const withdrawnAmount = this.withdraw(amount);
wallet.deposit(withdrawnAmount);
};

Wallet.prototype.resetDailyAllowance = function() {
this.dayTotalWithdrawals = 0;
};

Wallet.prototype.setDailyAllowance = function(newAllowance) {
this.dailyAllowance = newAllowance;
console.log(`Daily allowance set to: ${newAllowance}`);
};

Wallet.prototype.reportBalance = function () {
console.log(
`Name: ${this._name}, balance: ${eurosFormatter.format(this._cash)}`
Expand Down