Skip to content

Commit bd20fa1

Browse files
committed
In till.js function implemented.
1 parent 0088e75 commit bd20fa1

File tree

1 file changed

+20
-12
lines changed

1 file changed

+20
-12
lines changed

Sprint-2/stretch/till.js

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,35 @@
55
// Then it should return the total amount in pounds
66

77
function totalTill(till) {
8+
if (typeof till !== "object" || till === null || Array.isArray(till)) {
9+
throw new Error("Input should be an object");
10+
}
11+
812
let total = 0;
913

1014
for (const [coin, quantity] of Object.entries(till)) {
11-
total += coin * quantity;
15+
const value = parseInt(coin); // extracts the numeric part
16+
17+
if (isNaN(value)) continue;
18+
19+
total += value * quantity;
1220
}
1321

14-
return ${total / 100}`;
22+
const pounds = Math.floor(total / 100);
23+
const pence = String(total % 100).padStart(2, "0");
24+
25+
return ${pounds}.${pence}`;
1526
}
1627

17-
const till = {
18-
"1p": 10,
19-
"5p": 6,
20-
"50p": 4,
21-
"20p": 10,
22-
};
23-
const totalAmount = totalTill(till);
28+
module.exports = totalTill;
2429

2530
// a) What is the target output when totalTill is called with the till object
26-
31+
// --> The target output should be £4.40
2732
// b) Why do we need to use Object.entries inside the for...of loop in this function?
28-
33+
// --> To get both the key and value of the till object inside the loop.
2934
// c) What does coin * quantity evaluate to inside the for...of loop?
30-
35+
// --> It is supposed to calculate the total number of pence
3136
// d) Write a test for this function to check it works and then fix the implementation of totalTill
37+
38+
39+
// In till.js function implemented.

0 commit comments

Comments
 (0)