Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Samira w4 exercises #62

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

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++;
}
}
31 changes: 25 additions & 6 deletions Week4/prep-exercises/1-wallet/ex2-classes.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@ import eurosFormatter from './euroFormatter.js';
class Wallet {
#name;
#cash;
#dailyAllowance;
#dayTotalWithdrawals;

constructor(name, cash) {
this.#name = name;
this.#cash = cash;
this.#dailyAllowance = 40;
this.#dayTotalWithdrawals = 0;
}

get name() {
Expand Down Expand Up @@ -42,21 +46,36 @@ class Wallet {
`Name: ${this.name}, balance: ${eurosFormatter.format(this.#cash)}`
);
}
resetDailyAllowance() {
this.#dayTotalWithdrawals = 0;
console.log(
`Daily allowance reset to: ${eurosFormatter.format(this.#dailyAllowance)}`
);
}
setDailyAllowance(newAllowance) {
this.#dailyAllowance = newAllowance;
console.log(
`New daily withdrawal limit set to: ${eurosFormatter.format(this.#dailyAllowance)}`
);
}

}

function main() {
const walletJack = new Wallet('Jack', 100);
const walletJoe = new Wallet('Joe', 10);
const walletJane = new Wallet('Jane', 20);

walletJack.transferInto(walletJoe, 50);
walletJane.transferInto(walletJoe, 25);

walletJane.deposit(20);
walletJane.transferInto(walletJoe, 25);
walletJack.setDailyAllowance(70);
walletJack.withdraw(10);
walletJack.reportBalance();

walletJack.resetDailyAllowance();
walletJack.withdraw(20);
walletJack.reportBalance();
walletJoe.reportBalance();

walletJane.deposit(50);
walletJane.transferInto(walletJoe, 40);
walletJane.reportBalance();
}

Expand Down
48 changes: 39 additions & 9 deletions Week4/prep-exercises/1-wallet/ex3-object.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,25 @@ function createWallet(name, cash = 0) {
return {
_name: name,
_cash: cash,
_dailyAllowance: 40,
_dayTotalWithdrawals: 0,

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

withdraw: function (amount) {
if (this._dayTotalWithdrawals + amount > this._dailyAllowance) {
console.log(`Insufficient remaining daily allowance!`);
return 0;
}
if (this._cash - amount < 0) {
console.log(`Insufficient funds!`);
return 0;
}

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

Expand All @@ -38,23 +45,46 @@ function createWallet(name, cash = 0) {
getName: function () {
return this._name;
},
};
}
resetDailyAllowance: function () {
this._dayTotalWithdrawals = 0;
console.log(
`Daily allowance reset to: ${eurosFormatter.format(
this._dailyAllowance
)}`
);
},
setDailyAllowance: function (newAllowance) {
this._dailyAllowance = newAllowance;
console.log(
`New daily withdrawal limit set to: ${eurosFormatter.format(
this._dailyAllowance
)}`
);
},
}


}



function main() {
const walletJack = createWallet('Jack', 100);
const walletJoe = createWallet('Joe', 10);
const walletJane = createWallet('Jane', 20);

walletJack.transferInto(walletJoe, 50);
walletJane.transferInto(walletJoe, 25);

walletJane.deposit(20);
walletJane.transferInto(walletJoe, 25);

walletJack.setDailyAllowance(70);
walletJack.withdraw(10);
walletJack.reportBalance();
walletJoe.reportBalance();
walletJack.resetDailyAllowance();
walletJack.withdraw(20);
walletJack.reportBalance();

walletJane.deposit(50);
walletJane.transferInto(walletJoe, 40);
walletJane.reportBalance();
walletJoe.reportBalance();

}

main();
40 changes: 33 additions & 7 deletions Week4/prep-exercises/1-wallet/ex4-object-shared-methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,18 @@ function deposit(amount) {
}

function withdraw(amount) {
if (this._dayTotalWithdrawals + amount > this._dailyAllowance) {
console.log(`Insufficient remaining daily allowance!`);
return 0;
}

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

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

Expand All @@ -34,15 +40,33 @@ function getName() {
return this._name;
}

function resetDailyAllowance() {
this._dayTotalWithdrawals = 0;
console.log(
`Daily allowance reset to: ${eurosFormatter.format(this._dailyAllowance)}`
);
}

function setDailyAllowance(newAllowance) {
this._dailyAllowance = newAllowance;
console.log(
`New daily withdrawal limit set to: ${eurosFormatter.format(this._dailyAllowance)}`
);
}

function createWallet(name, cash = 0) {
return {
_name: name,
_cash: cash,
_dailyAllowance: 40,
_dayTotalWithdrawals: 0,
deposit,
withdraw,
transferInto,
reportBalance,
getName,
resetDailyAllowance,
setDailyAllowance,
};
}

Expand All @@ -51,15 +75,17 @@ function main() {
const walletJoe = createWallet('Joe', 10);
const walletJane = createWallet('Jane', 20);

walletJack.transferInto(walletJoe, 50);
walletJane.transferInto(walletJoe, 25);

walletJane.deposit(20);
walletJane.transferInto(walletJoe, 25);

walletJack.setDailyAllowance(70);
walletJack.withdraw(10);
walletJack.reportBalance();
walletJack.resetDailyAllowance();
walletJack.withdraw(20);
walletJack.reportBalance();
walletJoe.reportBalance();

walletJane.deposit(50);
walletJane.transferInto(walletJoe, 40);
walletJane.reportBalance();
walletJoe.reportBalance();
}

main();
36 changes: 30 additions & 6 deletions Week4/prep-exercises/1-wallet/ex5-prototype.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,19 @@ import eurosFormatter from './euroFormatter.js';
function Wallet(name, cash) {
this._name = name;
this._cash = cash;
this._dailyAllowance = 40;
this._dayTotalWithdrawals = 0;
}

Wallet.prototype.deposit = function (amount) {
this._cash += amount;
};

Wallet.prototype.withdraw = function (amount) {
if (this._dayTotalWithdrawals + amount > this._dailyAllowance) {
console.log(`Insufficient remaining daily allowance!`);
return 0;
}
if (this._cash - amount < 0) {
console.log(`Insufficient funds!`);
return 0;
Expand Down Expand Up @@ -39,20 +45,38 @@ Wallet.prototype.getName = function () {
return this._name;
};

Wallet.prototype.resetDailyAllowance = function () {
this._dayTotalWithdrawals = 0;
console.log(
`Daily allowance reset to: ${eurosFormatter.format(this._dailyAllowance)}`
);
}

Wallet.prototype.setDailyAllowance = function (newAllowance) {
this._dailyAllowance = newAllowance;
console.log(
`New daily withdrawal limit set to: ${eurosFormatter.format(this._dailyAllowance)}`
);
}

function main() {
const walletJack = new Wallet('Jack', 100);
const walletJoe = new Wallet('Joe', 10);
const walletJane = new Wallet('Jane', 20);

walletJack.transferInto(walletJoe, 50);
walletJane.transferInto(walletJoe, 25);

walletJane.deposit(20);
walletJane.transferInto(walletJoe, 25);
walletJack.setDailyAllowance(70);
walletJack.withdraw(10);
walletJack.reportBalance();

walletJack.resetDailyAllowance();
walletJack.withdraw(20);
walletJack.reportBalance();
walletJoe.reportBalance();

walletJane.deposit(50);
walletJane.transferInto(walletJoe, 40);
walletJane.reportBalance();
walletJoe.reportBalance();

}

main();