Skip to content

Commit 45df013

Browse files
author
Enice-Codes
committed
Complete till project
1 parent 4a78698 commit 45df013

2 files changed

Lines changed: 13 additions & 20 deletions

File tree

‎Sprint-2/stretch/till.js‎

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,14 @@
11
// totalTill takes an object representing coins in a till
2-
32
// Given an object of coins
43
// When this till object is passed to totalTill
54
// Then it should return the total amount in pounds
6-
75
function totalTill(till) {
86
let total = 0;
9-
107
for (const [coin, quantity] of Object.entries(till)) {
11-
total += coin * quantity;
8+
const coinValue = parseInt(coin, 10);
9+
total += coinValue * quantity;
1210
}
13-
1411
return `£${total / 100}`;
1512
}
1613

17-
const till = {
18-
"1p": 10,
19-
"5p": 6,
20-
"50p": 4,
21-
"20p": 10,
22-
};
23-
const totalAmount = totalTill(till);
24-
25-
// a) What is the target output when totalTill is called with the till object
26-
27-
// b) Why do we need to use Object.entries inside the for...of loop in this function?
28-
29-
// c) What does coin * quantity evaluate to inside the for...of loop?
30-
31-
// d) Write a test for this function to check it works and then fix the implementation of totalTill
14+
module.exports = totalTill;

‎Sprint-2/stretch/till.test.js‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const totalTill = require("./till");
2+
3+
test("calculates the total value of a till in pounds", () => {
4+
const till = { "1p": 10, "5p": 6, "50p": 4, "20p": 10 };
5+
expect(totalTill(till)).toBe("£4.4");
6+
});
7+
8+
test("returns £0 for an empty till", () => {
9+
expect(totalTill({})).toBe("£0");
10+
});

0 commit comments

Comments
 (0)