Skip to content
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
10 changes: 10 additions & 0 deletions .test-summary/TEST_SUMMARY.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
## Test Summary

**Mentors**: For more information on how to review homework assignments, please refer to the [Review Guide](https://github.com/HackYourFuture/mentors/blob/main/assignment-support/review-guide.md).

### 1-JavaScript - Week3

| Exercise | Passed | Failed | ESLint |
|----------------------------|--------|--------|--------|
| ex1-doubleEvenNumbers.test | 1 | - | ✓ |
| ex4-observable | 3 | - | ✓ |
13 changes: 13 additions & 0 deletions 1-JavaScript/Week3/assignment/ex1-doubleEvenNumbers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
function doubleEvenNumbers(numbers) {
// TODO rewrite the function body using `map` and `filter`.
return numbers.filter(n => n % 2 === 0).map(n => n * 2);
}
module.exports = doubleEvenNumbers;
// ! Unit test (using Jest)
describe('js-wk3-ex1-doubleEvenNumbers', () => {
test('doubleEvenNumbers should take the even numbers and double them', () => {
const actual = doubleEvenNumbers([1, 2, 3, 4]);
const expected = [4, 8];
expect(actual).toEqual(expected);
});
});
Comment on lines +1 to +13

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to be a duplicate file. In the future, please edit assignment and remove redundant code/files before submitting.

8 changes: 1 addition & 7 deletions 1-JavaScript/Week3/assignment/ex1-doubleEvenNumbers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,7 @@ Let's rewrite it (or _refactor_ it, as experienced developers would call it):
// ! Function to be tested
function doubleEvenNumbers(numbers) {
// TODO rewrite the function body using `map` and `filter`.
const newNumbers = [];
for (let i = 0; i < numbers.length; i++) {
if (numbers[i] % 2 === 0) {
newNumbers.push(numbers[i] * 2);
}
}
return newNumbers;
return numbers.filter(n => n % 2 === 0).map(n => n * 2);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great job using the filter() and map() method 😄

}

// ! Unit test (using Jest)
Expand Down
23 changes: 23 additions & 0 deletions 1-JavaScript/Week3/assignment/ex2-mondaysWorth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const eurosFormatter = new Intl.NumberFormat('nl-NL', {
style: 'currency',
currency: 'EUR',
});
Comment on lines +1 to +4

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: This returns a slightly different format than the expected value on the unit test (eg. '€NBSP187,50')


/**
* @param {Array<{duration:number}>} tasks
* @param {number} hourlyRate
* @returns {string}
*/
function computeEarnings(tasks, hourlyRate) {
const totalMinutes = Array.isArray(tasks)
? tasks.reduce((sum, t) => sum + (Number(t?.duration) || 0), 0)
: 0;

const totalHours = totalMinutes / 60;
const earnings = totalHours * Number(hourlyRate || 0);

return eurosFormatter.format(earnings);
}
Comment on lines +11 to +20

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good job using the reduce() method.

Challenge: How would you rewrite this function if you were to also apply the map() function, and return the required format without using the eurosFormatter() function?

Note: The tests were removed from the file. Please restore them.


module.exports = computeEarnings;

36 changes: 36 additions & 0 deletions 1-JavaScript/Week3/assignment/ex3-lemonAllergy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const sanitizeFruitBasket = require('./ex3-lemonAllergy');

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function sanitizeFruitBasket() seems to be incomplete. Could you try again?


describe('js-wk3-ex3-lemonAllergy', () => {
const fruitBasket = ['apple', 'banana', 'lemon', 'pear'];
const originalFruitBasketContents = [...fruitBasket];

test('sanitizeFruitBasket should take two parameters', () => {
expect(sanitizeFruitBasket.length === 2).toBe(true);
});

test('sanitizeFruitBasket should not modify the original fruitBasket array', () => {
sanitizeFruitBasket(fruitBasket, 'lemon');
const sameContent =
fruitBasket.length === originalFruitBasketContents.length &&
fruitBasket.every((x, i) => x === originalFruitBasketContents[i]);
expect(sameContent).toBe(true);
});

test('sanitizeFruitBasket should return a new array that does not include the unwanted "lemon"', () => {
const result = sanitizeFruitBasket(fruitBasket, 'lemon');
const hasNoLemon = !result
.map(x => String(x).toLowerCase().trim())
.includes('lemon');
expect(hasNoLemon).toBe(true);
});
});










50 changes: 50 additions & 0 deletions 1-JavaScript/Week3/assignment/ex4-observable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
function createObservable() {
const subscribers = new Set();

function subscribe(listener) {
if (typeof listener !== "function") {
return { unsubscribe() {} };
}
subscribers.add(listener);

return {
unsubscribe() {
subscribers.delete(listener);
},
};
}

function notify(message) {
// snapshot not needed with Set iteration, but this is fine
subscribers.forEach((fn) => fn(message));
}

return { subscribe, notify };
}

module.exports = createObservable;
function createObservable() {
const subscribers = [];

function subscribe(listener) {
subscribers.push(listener);
return {
unsubscribe() {
const index = subscribers.indexOf(listener);
if (index !== -1) {
subscribers.splice(index, 1);
}
},
};
}

function notify(message) {
for (const listener of subscribers) {
listener(message);
}
}

return { subscribe, notify };
}

module.exports = createObservable;
Comment on lines +1 to +50

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The solution is repeated twice in the file, and the solution is also listed in another file. Please edit your solutions before submitting your assignment. It's good practice for code maintenance.

31 changes: 12 additions & 19 deletions 1-JavaScript/Week3/assignment/ex4-observable/ex4-observable.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,18 @@
/*------------------------------------------------------------------------------
Full description at: https://github.com/HackYourFuture/Assignments/tree/main/1-JavaScript/Week4#exercise-4-observable
Complete the `createObservable()` function as follows:
function createObservable() {
const subscribers = [];

- The `subscribe` function should take the function passed to it as an argument
and push it onto the `subscribers` array. (Yes, you can store functions in an
array. Functions are treated in JavaScript like any other value.
function subscribe(listener) {
subscribers.push(listener);
}
Comment on lines +4 to +6

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


- The `notify` function should iterate through, and call, all subscribers from
the `subscribers` array, passing on the notification message to each
subscriber.
------------------------------------------------------------------------------*/
function notify(message) {
subscribers.forEach(listener => listener(message));
}
Comment on lines +8 to +10

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


export function createObservable() {
const subscribers = [];
return {
subscribe(subscriber) {
// TODO complete this function
},
notify(message) {
// TODO complete this function
},
subscribe,
notify
};
}

module.exports = { createObservable };
70 changes: 70 additions & 0 deletions 1-JavaScript/Week3/assignment/ex5-wallet.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
const eurosFormatter = new Intl.NumberFormat('nl-NL', {
style: 'currency',
currency: 'EUR',
});

function createWallet(name, cash = 0) {
function deposit(amount) {
cash += amount;
}

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

cash -= amount;
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 reportBalance() {
console.log(`Name: ${name}, balance: ${eurosFormatter.format(cash)}`);
}

const getName = () => name;

return {
deposit,
withdraw,
transferInto,
reportBalance,
getName,
};
}

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.reportBalance();
walletJoe.reportBalance();
walletJane.reportBalance();

// * End of exercise code

/*******************************************************************************/

// prettier-ignore
// eslint-disable-next-line no-unused-vars
const quiz = {
q1: { answer: 'b' },

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

q2: { answer: 'c' },

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

q3: { answer: 'a' },

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

q4: { answer: 'b' },

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❌ Please try again.

q5: { answer: 'c' },

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:+1

};
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
*** Unit Test Error Report ***

PASS 1-JavaScript/Week3/assignment/ex1-doubleEvenNumbers.test.js
js-wk3-ex1-doubleEvenNumbers
✅ doubleEvenNumbers should take the even numbers and double them (3 ms)

Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 0.635 s, estimated 1 s
Ran all test suites matching /C:\\Users\\Rimha\\Assignments-Cohort54\\1-JavaScript\\Week3\\assignment\\ex1-doubleEvenNumbers.test.js/i.
No linting errors detected.
No spelling errors detected.
15 changes: 15 additions & 0 deletions 1-JavaScript/Week3/test-reports/ex4-observable.report.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
*** Unit Test Error Report ***

PASS 1-JavaScript/Week3/assignment/ex4-observable/ex4-observable.test.js
js-wk3-ex4-observable
✅ createObservable should exist and be a function (2 ms)
✅ createObservable should return an object with `subscribe` and a `notify` function properties (1 ms)
✅ observable should notify all subscribers of any notification (1 ms)

Test Suites: 1 passed, 1 total
Tests: 3 passed, 3 total
Snapshots: 0 total
Time: 0.627 s, estimated 1 s
Ran all test suites matching /C:\\Users\\Rimha\\Assignments-Cohort54\\1-JavaScript\\Week3\\assignment\\ex4-observable\\ex4-observable.test.js/i.
No linting errors detected.
No spelling errors detected.