Skip to content

Commit 4a4cd94

Browse files
committed
completed till tests and function
1 parent cd89aa1 commit 4a4cd94

File tree

2 files changed

+69
-2
lines changed

2 files changed

+69
-2
lines changed

Sprint-2/stretch/till.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,24 @@
55
// Then it should return the total amount in pounds
66

77
function totalTill(till) {
8+
if (typeof till !== "object" || Array.isArray(till) || till === null) {
9+
throw new Error("Input should be an object");
10+
}
11+
12+
const validPence = ["1p", "5p", "20p", "50p"];
13+
814
let total = 0;
915

16+
console.log(Object.entries(till));
17+
1018
for (const [coin, quantity] of Object.entries(till)) {
11-
total += coin * quantity;
19+
if (validPence.indexOf(coin) === -1) {
20+
continue;
21+
}
22+
total += Number(coin.slice(0, coin.length - 1)) * quantity;
1223
}
1324

14-
return ${total / 100}`;
25+
return ${Math.floor(total / 100)}.${String(total % 100).padEnd(2, 0)}`;
1526
}
1627

1728
const till = {
@@ -22,10 +33,18 @@ const till = {
2233
};
2334
const totalAmount = totalTill(till);
2435

36+
module.exports = totalTill;
37+
2538
// a) What is the target output when totalTill is called with the till object
39+
// The target output is a string that state the total value of pound after converted from pence
40+
// For example: '£4.40'
2641

2742
// b) Why do we need to use Object.entries inside the for...of loop in this function?
43+
// We need to convert the object till into an array of arrays [ [ '1p', 10 ], [ '5p', 6 ], [ '50p', 4 ], [ '20p', 10 ] ]
44+
// That will enable the loop process in the for loop.
2845

2946
// c) What does coin * quantity evaluate to inside the for...of loop?
47+
// coin * quantity was trying to get the value of each group of pence and add to the total
48+
// However, coin is referencing to first element of each sub array, which is a string that can't do math operation
3049

3150
// d) Write a test for this function to check it works and then fix the implementation of totalTill

Sprint-2/stretch/till.test.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
const totalTill = require("./till");
2+
3+
test("Given a non object, it should throw error message", () => {
4+
const errorMessage = "Input should be an object";
5+
expect(() => totalTill([])).toThrow(errorMessage);
6+
expect(() => totalTill(123)).toThrow(errorMessage);
7+
expect(() => totalTill("123")).toThrow(errorMessage);
8+
expect(() => totalTill(null)).toThrow(errorMessage);
9+
expect(() => totalTill(undefined)).toThrow(errorMessage);
10+
expect(() => totalTill(true)).toThrow(errorMessage);
11+
});
12+
13+
test("Given an empty object, it should return £0.00", () => {
14+
expect(totalTill({})).toBe("£0.00");
15+
});
16+
17+
test("Given a pence object, it should return value of pound", () => {
18+
const till = {
19+
"1p": 10,
20+
"5p": 6,
21+
"50p": 4,
22+
"20p": 10,
23+
};
24+
expect(totalTill(till)).toBe("£4.40");
25+
});
26+
27+
test("Given a large amount of pence object, it should return proper value of pound", () => {
28+
const till = {
29+
"1p": 1234,
30+
"5p": 567,
31+
"50p": 890,
32+
"20p": 1011,
33+
};
34+
expect(totalTill(till)).toBe("£687.89");
35+
});
36+
37+
test("Given a pence object mixed with something else, it should ignore invalid input and return the value of pound", () => {
38+
const till = {
39+
"1p": 10,
40+
"5p": 6,
41+
fakeCoin: 10,
42+
undefined: 1,
43+
null: 2,
44+
"50p": 4,
45+
"20p": 10,
46+
};
47+
expect(totalTill(till)).toBe("£4.40");
48+
});

0 commit comments

Comments
 (0)