Skip to content

Commit e39f4df

Browse files
author
Enice-Codes
committed
Merge main into Data-Objects
2 parents a15e7d1 + 77ec0c3 commit e39f4df

1 file changed

Lines changed: 46 additions & 0 deletions

File tree

‎Sprint-2/stretch/till.js‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,49 @@ function totalTill(till) {
1212
}
1313

1414
module.exports = totalTill;
15+
16+
const till = {
17+
"1p": 10,
18+
"5p": 6,
19+
"50p": 4,
20+
"20p": 10,
21+
};
22+
const totalAmount = totalTill(till);
23+
24+
// a) What is the target output when totalTill is called with the till object
25+
"£1.20";
26+
27+
// b) Why do we need to use Object.entries inside the for...of loop in this function?
28+
// Object.entries() allows us to get both the coin and its quantity from the till object
29+
30+
// c) What does coin * quantity evaluate to inside the for...of loop?
31+
// It evaluates to the total value of that particular coin type in pence
32+
33+
// d) Write a test for this function to check it works and then fix the implementation of totalTill
34+
// test function
35+
36+
test("totalTill returns the correct total", () => {
37+
const till = {
38+
"1p": 10,
39+
"5p": 6,
40+
"50p": 4,
41+
"20p": 10,
42+
};
43+
44+
expect(totalTill(till)).toBe("£4.40");
45+
});
46+
47+
// fixed implementation
48+
49+
function totalTill(till) {
50+
let total = 0;
51+
52+
for (const [coin, quantity] of Object.entries(till)) {
53+
const coinValue = parseInt(coin);
54+
total += coinValue * quantity;
55+
}
56+
57+
return `£${(total / 100).toFixed(2)}`;
58+
}
59+
// the important fix
60+
const coinValue = parseInt(coin);

0 commit comments

Comments
 (0)