Skip to content

Commit 3c3eb40

Browse files
Finished ex1, ex2, ex3, ex4 for assignment week 3
1 parent c50e3ce commit 3c3eb40

File tree

9 files changed

+99
-21
lines changed

9 files changed

+99
-21
lines changed

.test-summary/TEST_SUMMARY.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
## Test Summary
2+
3+
**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).
4+
5+
### 1-JavaScript - Week3
6+
7+
| Exercise | Passed | Failed | ESLint |
8+
|----------------------------|--------|--------|--------|
9+
| ex1-doubleEvenNumbers.test | 1 | - ||
10+
| ex2-mondaysWorth.test | 2 | - ||
11+
| ex3-lemonAllergy.test | 3 | - ||
12+
| ex4-observable | 3 | - ||

1-JavaScript/Week3/assignment/ex1-doubleEvenNumbers.test.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,9 @@ Let's rewrite it (or _refactor_ it, as experienced developers would call it):
1111
------------------------------------------------------------------------------*/
1212
// ! Function to be tested
1313
function doubleEvenNumbers(numbers) {
14-
// TODO rewrite the function body using `map` and `filter`.
15-
const newNumbers = [];
16-
for (let i = 0; i < numbers.length; i++) {
17-
if (numbers[i] % 2 === 0) {
18-
newNumbers.push(numbers[i] * 2);
19-
}
20-
}
21-
return newNumbers;
14+
return numbers
15+
.filter(number => number % 2 === 0)
16+
.map(number => number * 2)
2217
}
2318

2419
// ! Unit test (using Jest)

1-JavaScript/Week3/assignment/ex2-mondaysWorth.test.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,15 @@ const mondayTasks = [
3030

3131
const hourlyRate = 25;
3232

33-
function computeEarnings(/* TODO parameter(s) go here */) {
34-
// TODO complete this function
33+
function computeEarnings(mondayTasks, hourlyRate) {
34+
const totalDuration = mondayTasks.reduce((acc, current) => {
35+
return acc + current.duration
36+
}, 0)
37+
38+
const totalHours = totalDuration / 60
39+
const totalAmount = (totalHours * hourlyRate).toFixed(2)
40+
41+
return `€${totalAmount}`
3542
}
3643

3744
// ! Unit tests (using Jest)

1-JavaScript/Week3/assignment/ex3-lemonAllergy.test.js

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,26 +25,31 @@ const fruitBasket = [
2525
];
2626

2727
// ! Function under test
28-
function sanitizeFruitBasket(/* TODO parameter(s) go here */) {
29-
// TODO complete this function
28+
function sanitizeFruitBasket(fruitBasket, sanitizeFruit) {
29+
return fruitBasket.filter(fruit => fruit !== sanitizeFruit)
3030
}
3131

3232
// ! Unit tests (using Jest)
3333
describe('js-wk3-ex3-lemonAllergy', () => {
3434
test('sanitizeFruitBasket should take two parameters', () => {
35-
// TODO replace next line with your code
36-
expect(false).toBe(true);
35+
expect(sanitizeFruitBasket).toHaveLength(2)
3736
});
3837

3938
test('sanitizeFruitBasket should not modify the original `fruitBasket` array', () => {
4039
// Save the original contents of the fruit basket
41-
const originalFruitBasketContents = [...fruitBasket];
42-
// TODO replace next line with your code
43-
expect(false).toBe(true);
40+
const originalFruitBasketContents = [...fruitBasket]
41+
sanitizeFruitBasket(fruitBasket, 'lemon')
42+
expect(fruitBasket).toEqual(originalFruitBasketContents)
4443
});
4544

4645
test('sanitizeFruitBasket should return a new array that does not include the unwanted `lemon`', () => {
47-
// TODO replace next line with your code
48-
expect(false).toBe(true);
46+
const sanitizeArray = [
47+
'apple',
48+
'grapefruit',
49+
'banana',
50+
'watermelon',
51+
]
52+
const newArray = sanitizeFruitBasket(fruitBasket, 'lemon')
53+
expect(newArray).toEqual(sanitizeArray);
4954
});
5055
});

1-JavaScript/Week3/assignment/ex4-observable/ex4-observable.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@ export function createObservable() {
1616
const subscribers = [];
1717
return {
1818
subscribe(subscriber) {
19-
// TODO complete this function
19+
subscribers.push(subscriber)
2020
},
2121
notify(message) {
22-
// TODO complete this function
22+
subscribers.forEach(subscriber => {
23+
subscriber(message)
24+
})
2325
},
2426
};
2527
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
*** Unit Test Error Report ***
2+
3+
PASS 1-JavaScript/Week3/assignment/ex1-doubleEvenNumbers.test.js
4+
js-wk3-ex1-doubleEvenNumbers
5+
✅ doubleEvenNumbers should take the even numbers and double them (1 ms)
6+
7+
Test Suites: 1 passed, 1 total
8+
Tests: 1 passed, 1 total
9+
Snapshots: 0 total
10+
Time: 0.251 s, estimated 1 s
11+
Ran all test suites matching /\/Users\/dimadoronin\/Documents\/Programming\/HackYourFuture\/Assignments-Cohort54\/1-JavaScript\/Week3\/assignment\/ex1-doubleEvenNumbers.test.js/i.
12+
No linting errors detected.
13+
No spelling errors detected.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
*** Unit Test Error Report ***
2+
3+
PASS 1-JavaScript/Week3/assignment/ex2-mondaysWorth.test.js
4+
js-wk3-mondaysWorth
5+
✅ computeEarnings should take two parameters (1 ms)
6+
✅ computeEarnings should compute the earnings as a formatted Euro amount
7+
8+
Test Suites: 1 passed, 1 total
9+
Tests: 2 passed, 2 total
10+
Snapshots: 0 total
11+
Time: 0.209 s, estimated 1 s
12+
Ran all test suites matching /\/Users\/dimadoronin\/Documents\/Programming\/HackYourFuture\/Assignments-Cohort54\/1-JavaScript\/Week3\/assignment\/ex2-mondaysWorth.test.js/i.
13+
No linting errors detected.
14+
No spelling errors detected.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
*** Unit Test Error Report ***
2+
3+
PASS 1-JavaScript/Week3/assignment/ex3-lemonAllergy.test.js
4+
js-wk3-ex3-lemonAllergy
5+
✅ sanitizeFruitBasket should take two parameters (1 ms)
6+
✅ sanitizeFruitBasket should not modify the original `fruitBasket` array (1 ms)
7+
✅ sanitizeFruitBasket should return a new array that does not include the unwanted `lemon`
8+
9+
Test Suites: 1 passed, 1 total
10+
Tests: 3 passed, 3 total
11+
Snapshots: 0 total
12+
Time: 0.246 s, estimated 1 s
13+
Ran all test suites matching /\/Users\/dimadoronin\/Documents\/Programming\/HackYourFuture\/Assignments-Cohort54\/1-JavaScript\/Week3\/assignment\/ex3-lemonAllergy.test.js/i.
14+
No linting errors detected.
15+
No spelling errors detected.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
*** Unit Test Error Report ***
2+
3+
PASS 1-JavaScript/Week3/assignment/ex4-observable/ex4-observable.test.js
4+
js-wk3-ex4-observable
5+
✅ createObservable should exist and be a function (1 ms)
6+
✅ createObservable should return an object with `subscribe` and a `notify` function properties
7+
✅ observable should notify all subscribers of any notification (1 ms)
8+
9+
Test Suites: 1 passed, 1 total
10+
Tests: 3 passed, 3 total
11+
Snapshots: 0 total
12+
Time: 0.216 s, estimated 1 s
13+
Ran all test suites matching /\/Users\/dimadoronin\/Documents\/Programming\/HackYourFuture\/Assignments-Cohort54\/1-JavaScript\/Week3\/assignment\/ex4-observable\/ex4-observable.test.js/i.
14+
No linting errors detected.
15+
No spelling errors detected.

0 commit comments

Comments
 (0)