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
56 lines (51 loc) · 1.92 KB
/
ex2-mondaysWorth.test.js
File metadata and controls
56 lines (51 loc) · 1.92 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
54
55
56
const { duration } = require('moment/moment');
/*------------------------------------------------------------------------------
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, rate /* TODO parameter(s) go here */) {
// TODO complete this function
const hourlyRate = tasks
.map((task) => (task.duration / 60) * rate)
.reduce((sum, num) => sum + num);
return `€${hourlyRate.toFixed(2)}`;
}
//console.log(computeEarnings(mondayTasks, hourlyRate));
// ! 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);
});
});