File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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-
75function 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 ;
Original file line number Diff line number Diff line change 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+ });
You can’t perform that action at this time.
0 commit comments