Skip to content

Commit e13713e

Browse files
committed
till function and testing it
1 parent eac17d1 commit e13713e

File tree

3 files changed

+107
-7
lines changed

3 files changed

+107
-7
lines changed

Sprint-2/stretch/mode.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,39 @@ function calculateMode(list) {
3434
}
3535

3636
module.exports = calculateMode;
37+
38+
// Refactoring
39+
40+
function trackFrequency(list) {
41+
let frequency = new Map();
42+
43+
for (const num of list) {
44+
if (typeof num !== "number") {
45+
continue;
46+
}
47+
if (frequency.has(num)) {
48+
frequency.set(num, (frequency.get(num) || 0) + 1);
49+
} else {
50+
frequency.set(num, 1);
51+
}
52+
}
53+
return frequency;
54+
}
55+
56+
function findMode(frequency) {
57+
let maxFreq = 0;
58+
let mode = NaN;
59+
60+
for (const [num, freq] of frequency) {
61+
if (freq > maxFreq) {
62+
maxFreq = freq;
63+
mode = num;
64+
}
65+
}
66+
return mode;
67+
}
68+
69+
function calculateModeRef(list) {
70+
const frequency = trackFrequency(list);
71+
return findMode(frequency);
72+
}

Sprint-2/stretch/till.js

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

77
function totalTill(till) {
8-
let total = 0;
9-
10-
for (const [coin, quantity] of Object.entries(till)) {
11-
total += coin * quantity;
8+
if (till === 0) {
9+
return "0.00";
10+
} else if (typeof till !== "object" || till === null || Array.isArray(till)) {
11+
throw new Error("The input is not a valid object");
12+
} else {
13+
let total = 0;
14+
for (let [coin, quantity] of Object.entries(till)) {
15+
//transform till object into an array and
16+
// looping trough coin as a key and quantity as value
17+
18+
if (coin.endsWith("p")) {
19+
coin = Number(coin.slice(0, -1)); // rid of "p" at the end of coin
20+
} else if (coin.startsWith("£")) {
21+
coin = Number(coin.slice(1)) * 100; // rid of "£" at the start of coin and transform pound to coin
22+
// by multiple by 100
23+
} else {
24+
throw new Error("Invalid coin format");
25+
}
26+
27+
if (isNaN(coin)) {
28+
throw new Error("Invalid coin format");
29+
}
30+
total += coin * quantity; // calculate total
31+
}
32+
return ${(total / 100).toFixed(2)}`; // transform pence into pounds and round total to two decimal places
1233
}
13-
14-
return ${total / 100}`;
1534
}
1635

1736
const till = {
1837
"1p": 10,
1938
"5p": 6,
2039
"50p": 4,
21-
"20p": 10,
40+
"20p": 0,
2241
};
42+
2343
const totalAmount = totalTill(till);
44+
console.log(totalAmount);
2445

2546
// a) What is the target output when totalTill is called with the till object
47+
//A total amount in pounds: e.g. £2.40, represented by pounds and coins with two decimal places.
2648

2749
// b) Why do we need to use Object.entries inside the for...of loop in this function?
50+
// We need to use the Object.entries to transform the object into an array to get the ability
51+
// to pull keys and values at the same time from the till object to calculate the total.
52+
// e.g. 1p and 10
2853

2954
// c) What does coin * quantity evaluate to inside the for...of loop?
55+
// The coin and quantity evaluate to inside the for...of loop, the total pounds in pence
56+
// according to the type of coin in each array inside the Object.entries(till) array.
3057

3158
// d) Write a test for this function to check it works and then fix the implementation of totalTill
59+
60+
module.exports = totalTill;

Sprint-2/stretch/till.test.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
const totalTill = require("./till.js");
2+
3+
describe("edge cases for till function", () => {
4+
test("given empty object", () => {
5+
const currentOutput = totalTill({});
6+
const targetOutput = "£0.00";
7+
expect(currentOutput).toEqual(targetOutput);
8+
});
9+
});
10+
11+
test("given empty object", () => {
12+
const currentOutput = totalTill({ "£0.1": 1, "0.20p": 10 });
13+
const targetOutput = "£0.12";
14+
expect(currentOutput).toEqual(targetOutput);
15+
});
16+
17+
test("given number as input throw an error", () => {
18+
expect(() => totalTill(1233)).toThrow("The input is not a valid object");
19+
});
20+
21+
test("given input with 0-quantities as input", () => {
22+
const currentOutput = totalTill({ "1p": 0, "20p": 0 });
23+
const targetOutput = "£0.00";
24+
expect(currentOutput).toEqual(targetOutput);
25+
});
26+
27+
test("given null as input throw an error", () => {
28+
expect(() => totalTill(null)).toThrow("The input is not a valid object");
29+
});
30+
31+
test("given null as input throw an error", () => {
32+
expect(() => totalTill(["1p", 10, "20p", 9])).toThrow(
33+
"The input is not a valid object"
34+
);
35+
});

0 commit comments

Comments
 (0)