Skip to content

Commit 77ec0c3

Browse files
author
Enice-Codes
committed
answered questions from file
1 parent de6889c commit 77ec0c3

1 file changed

Lines changed: 30 additions & 0 deletions

File tree

‎Sprint-2/stretch/till.js‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,39 @@ const till = {
2323
const totalAmount = totalTill(till);
2424

2525
// a) What is the target output when totalTill is called with the till object
26+
"£1.20";
2627

2728
// b) Why do we need to use Object.entries inside the for...of loop in this function?
29+
// Object.entries() allows us to get both the coin and its quantity from the till object
2830

2931
// c) What does coin * quantity evaluate to inside the for...of loop?
32+
// It evaluates to the total value of that particular coin type in pence
3033

3134
// d) Write a test for this function to check it works and then fix the implementation of totalTill
35+
// test function
36+
37+
test("totalTill returns the correct total", () => {
38+
const till = {
39+
"1p": 10,
40+
"5p": 6,
41+
"50p": 4,
42+
"20p": 10,
43+
};
44+
45+
expect(totalTill(till)).toBe("£4.40");
46+
});
47+
48+
// fixed implementation
49+
50+
function totalTill(till) {
51+
let total = 0;
52+
53+
for (const [coin, quantity] of Object.entries(till)) {
54+
const coinValue = parseInt(coin);
55+
total += coinValue * quantity;
56+
}
57+
58+
return `£${(total / 100).toFixed(2)}`;
59+
}
60+
// the important fix
61+
const coinValue = parseInt(coin);

0 commit comments

Comments
 (0)