Skip to content

Commit 18d34ec

Browse files
till js
1 parent 8f12144 commit 18d34ec

1 file changed

Lines changed: 8 additions & 9 deletions

File tree

‎Sprint-2/stretch/till.js‎

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,23 @@ function totalTill(till) {
88
let total = 0;
99

1010
for (const [coin, quantity] of Object.entries(till)) {
11-
total += coin * quantity;
11+
const coinValue = parseInt(coin, 10);
12+
total += coinValue * quantity;
1213
}
1314

1415
return `£${total / 100}`;
1516
}
1617

17-
const till = {
18-
"1p": 10,
19-
"5p": 6,
20-
"50p": 4,
21-
"20p": 10,
22-
};
23-
const totalAmount = totalTill(till);
24-
2518
// a) What is the target output when totalTill is called with the till object
19+
// a) The target output is £4.4
2620

2721
// b) Why do we need to use Object.entries inside the for...of loop in this function?
22+
// b) Object.entries converts the object into key-value pairs,
23+
// allowing us to access both the coin and its quantity.
2824

2925
// c) What does coin * quantity evaluate to inside the for...of loop?
26+
// c) coinValue * quantity calculates the total value
27+
// of that type of coin.
3028

3129
// d) Write a test for this function to check it works and then fix the implementation of totalTill
30+
module.exports = totalTill;

0 commit comments

Comments
 (0)