-
-
Notifications
You must be signed in to change notification settings - Fork 283
Expand file tree
/
Copy pathtill.js
More file actions
43 lines (33 loc) · 1.14 KB
/
till.js
File metadata and controls
43 lines (33 loc) · 1.14 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
// totalTill takes an object representing coins in a till
// Given an object of coins
// When this till object is passed to totalTill
// Then it should return the total amount in pounds
function totalTill(till) {
let total = 0;
for (const [coin, quantity] of Object.entries(till)) {
const valueOfCoin = Number(coin.slice(0, -1));
total += valueOfCoin * quantity;
}
return `£${(total / 100).toFixed(2)}`;
}
const till = {
"1p": 10,
"5p": 6,
"50p": 4,
"20p": 10,
};
const totalAmount = totalTill(till);
const assertEquals = (actual, expected) => {
console.assert(
actual === expected,
`Received ${actual} but expected ${expected}.`
);
};
// a) What is the target output when totalTill is called with the till object
// £4.4
// b) Why do we need to use Object.entries inside the for...of loop in this function?
// to iterate in an object and access the [key, value] pair
// c) What does coin * quantity evaluate to inside the for...of loop?
// it multiplies keys and values of till
// d) Write a test for this function to check it works and then fix the implementation of totalTill
assertEquals(totalAmount, "£4.40");