Skip to content

Commit f5a92f8

Browse files
author
Fares Bakhet
committed
Answering till.js questions
1 parent d5a5bcd commit f5a92f8

File tree

1 file changed

+25
-3
lines changed

1 file changed

+25
-3
lines changed

Sprint-2/stretch/till.js

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,32 @@ const till = {
2222
};
2323
const totalAmount = totalTill(till);
2424

25-
// a) What is the target output when totalTill is called with the till object
25+
// a) What is the target output when totalTill is called with the till object >> "£4.40"
2626

27-
// b) Why do we need to use Object.entries inside the for...of loop in this function?
27+
// b) Why do we need to use Object.entries inside the for...of loop in this function? >> To iterate over both the keys (coin types) and values (quantities) of the till object.
2828

29-
// c) What does coin * quantity evaluate to inside the for...of loop?
29+
// c) What does coin * quantity evaluate to inside the for...of loop? >> It evaluates to NaN because coin is a string (e.g., "1p") and cannot be directly multiplied by a number (quantity).
3030

3131
// d) Write a test for this function to check it works and then fix the implementation of totalTill
32+
33+
// Fixed implementation
34+
function totalTillFixed(till) {
35+
let total = 0;
36+
37+
for (const [coin, quantity] of Object.entries(till)) {
38+
const coinValue = parseInt(coin);
39+
total += coinValue * quantity;
40+
}
41+
42+
return ${(total / 100).toFixed(2)}`;
43+
}
44+
45+
// Test
46+
const tillTest = {
47+
"1p": 10,
48+
"5p": 6,
49+
"50p": 4,
50+
"20p": 10,
51+
};
52+
const totalAmountTest = totalTillFixed(tillTest);
53+
console.log(totalAmountTest);

0 commit comments

Comments
 (0)