forked from HackYourFuture/Assignments
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathex2-mondaysWorth.test.js
More file actions
53 lines (48 loc) · 1.77 KB
/
ex2-mondaysWorth.test.js
File metadata and controls
53 lines (48 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/*------------------------------------------------------------------------------
Full description atL https://github.com/HackYourFuture/Assignments/tree/main/1-JavaScript/Week4#exercise-2-whats-your-monday-worth
- Complete the function names `computeEarnings`. It should take an array of
tasks and an hourly rate as arguments and return a formatted Euro amount
(e.g: `€11.34`) comprising the total earnings.
- Use the `map` array function to take out the duration time for each task.
- Multiply each duration by a hourly rate for billing and sum it all up.
- Make sure the program can be used on any array of objects that contain a
`duration` property with a number value.
------------------------------------------------------------------------------*/
const mondayTasks = [
{
name: 'Daily standup',
duration: 30, // specified in minutes
},
{
name: 'Feature discussion',
duration: 120,
},
{
name: 'Development time',
duration: 240,
},
{
name: 'Talk to different members from the product team',
duration: 60,
},
];
const hourlyRate = 25;
function computeEarnings(tasks, hourlyRate) {
const total = tasks
.map(task => task.duration / 60 * hourlyRate)
.reduce((sum, value) => sum + value, 0);
return `€${total.toFixed(2)}`;
}
// ! Unit tests (using Jest)
describe('js-wk3-mondaysWorth', () => {
test('computeEarnings should take two parameters', () => {
// The `.length` property indicates the number of parameters expected by
// the function.
expect(computeEarnings).toHaveLength(2);
});
test('computeEarnings should compute the earnings as a formatted Euro amount', () => {
const result = computeEarnings(mondayTasks, hourlyRate);
const expected = '€187.50';
expect(result).toBe(expected);
});
});