Skip to content

Commit 64a4014

Browse files
author
Enice-Codes
committed
fix till.js merge duplication and formatting, restore invert.js explanations
1 parent a901b1f commit 64a4014

3 files changed

Lines changed: 58 additions & 86 deletions

File tree

‎Sprint-2/implement/tally.test.js‎

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -14,66 +14,22 @@ const tally = require("./tally.js");
1414
* tally(['a', 'a', 'b', 'c']), target output: { a : 2, b: 1, c: 1 }
1515
*/
1616

17-
// Acceptance criteria:
1817

1918
// Given a function called tally
2019
// When passed an array of items
2120
// Then it should return an object containing the count for each unique item
2221
describe("tally on an array of items returns counts for each unique item", () => {
23-
// Given an empty array
24-
// When passed to tally
25-
// Then it should return an empty object
2622
test("tally on an empty array returns an empty object", () => {
2723
expect(tally([])).toEqual({});
2824
});
2925

30-
<<<<<<< HEAD
31-
// Given an array with duplicate items
32-
// When passed to tally
33-
// Then it should return counts for each unique item
3426
test("tally on an array with duplicate items returns counts for each unique item", () => {
3527
expect(tally(["a"])).toEqual({ a: 1 });
3628
expect(tally(["a", "a", "a"])).toEqual({ a: 3 });
3729
expect(tally(["a", "a", "b", "c"])).toEqual({ a: 2, b: 1, c: 1 });
3830
});
3931

40-
// Given an invalid input like a string
41-
// When passed to tally
42-
// Then it should throw an error
4332
test("tally throws an error for invalid input", () => {
4433
expect(() => tally("not an array")).toThrow();
4534
});
46-
=======
47-
test("tally on an empty array returns an empty object", () => {
48-
expect(tally([])).toEqual({});
49-
});
50-
51-
// Given an empty array
52-
// When passed to tally
53-
// Then it should return an empty object
54-
test("tally on an array with duplicate items returns counts for each unique item", () => {
55-
expect(tally(["a"])).toEqual({ a: 1 });
56-
expect(tally(["a", "a", "a"])).toEqual({ a: 3 });
57-
expect(tally(["a", "a", "b", "c"])).toEqual({ a: 2, b: 1, c: 1 });
58-
});
59-
60-
// Given an array with duplicate items
61-
// When passed to tally
62-
// Then it should return counts for each unique item
63-
64-
test("tally on an array with duplicate items returns counts for each unique item", () => {
65-
expect(tally(["a"])).toEqual({ a: 1 });
66-
expect(tally(["a", "a", "a"])).toEqual({ a: 3 });
67-
expect(tally(["a", "a", "b", "c"])).toEqual({ a: 2, b: 1, c: 1 });
68-
});
69-
70-
71-
// Given an invalid input like a string
72-
// When passed to tally
73-
// Then it should throw an error
74-
test("tally on an array with duplicate items returns counts for each unique item", () => {
75-
expect(tally(["a"])).toEqual({ a: 1 });
76-
expect(tally(["a", "a", "a"])).toEqual({ a: 3 });
77-
expect(tally(["a", "a", "b", "c"])).toEqual({ a: 2, b: 1, c: 1 });
78-
>>>>>>> Data-Objects
7935
});

‎Sprint-2/interpret/invert.js‎

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,48 @@
44
// When invert is passed this object
55
// Then it should swap the keys and values in the object
66
// E.g. invert({x : 10, y : 20}), target output: {"10": "x", "20": "y"}
7+
8+
// The original buggy implementation looked like this:
9+
//
10+
// function invert(obj) {
11+
// const invertedObj = {};
12+
// for (const [key, value] of Object.entries(obj)) {
13+
// invertedObj.key = value;
14+
// invertedObj[value] = key;
15+
// }
16+
// return invertedObj;
17+
// }
18+
19+
// a) What is the current return value when invert is called with { a: 1 }?
20+
// -> { key: 1, "1": "a" }
21+
// The line `invertedObj.key = value` sets a property literally named
22+
// "key" (not the loop variable's value) to 1, alongside the correct
23+
// inverted pair "1": "a" from the line below it.
24+
25+
// b) What is the current return value when invert is called with { a: 1, b: 2 }?
26+
// -> { key: 2, "1": "a", "2": "b" }
27+
// Each loop iteration overwrites the same literal "key" property, so
28+
// only the last value assigned to it survives - here, 2 from { b: 2 }.
29+
30+
// c) What does Object.entries return, and why is it needed?
31+
// -> Object.entries(obj) returns an array of [key, value] pairs, e.g.
32+
// [["a", 1], ["b", 2]]. It's needed because a for...of loop can't
33+
// iterate directly over an object's properties - Object.entries
34+
// converts the object into something iterable, and array
35+
// destructuring ([key, value]) lets us pull out both parts at once.
36+
37+
// d) Why is the current return value different from the target output?
38+
// -> The bug is `invertedObj.key = value`. Because "key" is written as a
39+
// literal property name (dot notation), it always sets a property
40+
// called "key" rather than using the value of the loop variable
41+
// `key`. Only bracket notation - invertedObj[key] - would use the
42+
// variable's actual value as the property name. This line also isn't
43+
// needed at all for a correct inversion; it should be removed.
44+
45+
// e) Fix: remove the incorrect `invertedObj.key = value` line entirely,
46+
// leaving only `invertedObj[value] = key`, which correctly maps each
47+
// value to its original key. See invert.test.js for tests proving
48+
// the fix works, including empty objects and string values.
749
function invert(obj) {
850
const invertedObj = {};
951
for (const [key, value] of Object.entries(obj)) {
@@ -12,4 +54,4 @@ function invert(obj) {
1254
return invertedObj;
1355
}
1456

15-
module.exports = invert;
57+
module.exports = invert;

‎Sprint-2/stretch/till.js‎

Lines changed: 15 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,59 +2,33 @@
22
// Given an object of coins
33
// When this till object is passed to totalTill
44
// Then it should return the total amount in pounds
5-
function totalTill(till) {
6-
let total = 0;
7-
for (const [coin, quantity] of Object.entries(till)) {
8-
const coinValue = parseInt(coin, 10);
9-
total += coinValue * quantity;
10-
}
11-
return `£${total / 100}`;
12-
}
13-
14-
module.exports = totalTill;
15-
16-
const till = {
17-
"1p": 10,
18-
"5p": 6,
19-
"50p": 4,
20-
"20p": 10,
21-
};
22-
const totalAmount = totalTill(till);
235

246
// a) What is the target output when totalTill is called with the till object
25-
"£1.20";
7+
// { "1p": 10, "5p": 6, "50p": 4, "20p": 10 }?
8+
// -> "£4.40" - 10p (1p x10) + 30p (5p x6) + 200p (50p x4) + 200p (20p x10)
9+
// = 440p total = £4.40
2610

2711
// b) Why do we need to use Object.entries inside the for...of loop in this function?
28-
// Object.entries() allows us to get both the coin and its quantity from the till object
29-
30-
// c) What does coin * quantity evaluate to inside the for...of loop?
31-
// It evaluates to the total value of that particular coin type in pence
32-
33-
// d) Write a test for this function to check it works and then fix the implementation of totalTill
34-
// test function
12+
// -> Object.entries(till) returns an array of [coin, quantity] pairs so
13+
// both values can be destructured together in each loop iteration.
3514

36-
test("totalTill returns the correct total", () => {
37-
const till = {
38-
"1p": 10,
39-
"5p": 6,
40-
"50p": 4,
41-
"20p": 10,
42-
};
15+
// c) What does coinValue * quantity evaluate to inside the for...of loop?
16+
// -> The total value, in pence, contributed by that one coin denomination.
4317

44-
expect(totalTill(till)).toBe("£4.40");
45-
});
46-
47-
// fixed implementation
18+
// d) Write a test for this function to check it works and then fix the
19+
// implementation of totalTill
20+
// -> See till.test.js. The fix: parse each coin string to a number with
21+
// parseInt before multiplying, and format the result to 2 decimal places.
4822

4923
function totalTill(till) {
5024
let total = 0;
5125

5226
for (const [coin, quantity] of Object.entries(till)) {
53-
const coinValue = parseInt(coin);
27+
const coinValue = parseInt(coin, 10);
5428
total += coinValue * quantity;
5529
}
5630

57-
return `£${(total / 100).toFixed(2)}`;
31+
return `£${parseFloat((total / 100).toFixed(2))}`;
5832
}
59-
// the important fix
60-
const coinValue = parseInt(coin);
33+
34+
module.exports = totalTill;

0 commit comments

Comments
 (0)