Skip to content

Commit af702c4

Browse files
committed
Fix invert implementation and add tests
1 parent 7aa84ed commit af702c4

2 files changed

Lines changed: 86 additions & 13 deletions

File tree

Sprint-2/interpret/invert.js

Lines changed: 66 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,79 @@
44
// When invert is passed this object
55
// Then it should swap the keys and values in the object
66

7-
// E.g. invert({x : 10, y : 20}), target output: {"10": "x", "20": "y"}
7+
// Example:
8+
// invert({ x: 10, y: 20 })
9+
// should return:
10+
// { "10": "x", "20": "y" }
811

9-
function invert(obj) {
10-
const invertedObj = {};
12+
/*
13+
a) What is the current return value when invert({ a: 1 }) is called?
1114
12-
for (const [key, value] of Object.entries(obj)) {
13-
invertedObj.key = value;
14-
}
15+
Current output:
16+
{ key: 1 }
1517
16-
return invertedObj;
18+
Reason:
19+
The code uses invertedObj.key which creates a property literally called "key"
20+
instead of using the variable key.
21+
22+
---------------------------------------------------
23+
24+
b) What is the current return value when invert({ a: 1, b: 2 }) is called?
25+
26+
Current output:
27+
{ key: 2 }
28+
29+
Reason:
30+
The second iteration overwrites the first value because the property name
31+
is always "key".
32+
33+
---------------------------------------------------
34+
35+
c) What is the target return value when invert({ a: 1, b: 2 }) is called?
36+
37+
Expected output:
38+
{
39+
"1": "a",
40+
"2": "b"
1741
}
1842
19-
// a) What is the current return value when invert is called with { a : 1 }
43+
Keys and values should be swapped.
44+
45+
---------------------------------------------------
2046
21-
// b) What is the current return value when invert is called with { a: 1, b: 2 }
47+
d) What does Object.entries return? Why is it needed?
2248
23-
// c) What is the target return value when invert is called with {a : 1, b: 2}
49+
Object.entries(obj) converts an object into an array of [key, value] pairs.
2450
25-
// c) What does Object.entries return? Why is it needed in this program?
51+
Example:
52+
Object.entries({ a: 1, b: 2 })
2653
27-
// d) Explain why the current return value is different from the target output
54+
returns:
55+
[
56+
["a", 1],
57+
["b", 2]
58+
]
59+
60+
This allows us to loop through object properties using for...of.
61+
62+
---------------------------------------------------
63+
64+
e) Why is the current return value different from the target output?
65+
66+
Because invertedObj.key uses the literal property name "key".
67+
To use the value dynamically as the key, we must use bracket notation:
68+
invertedObj[value] = key;
69+
*/
70+
71+
function invert(obj) {
72+
const invertedObj = {};
73+
74+
for (const [key, value] of Object.entries(obj)) {
75+
// Correct implementation: swap key and value
76+
invertedObj[value] = key;
77+
}
78+
79+
return invertedObj;
80+
}
2881

29-
// e) Fix the implementation of invert (and write tests to prove it's fixed!)
82+
module.exports = invert;

Sprint-2/interpret/invert.test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const invert = require("./invert.js");
2+
3+
describe("invert()", () => {
4+
test("inverts a single key-value pair", () => {
5+
expect(invert({ a: 1 })).toEqual({
6+
1: "a",
7+
});
8+
});
9+
10+
test("inverts multiple key-value pairs", () => {
11+
expect(invert({ a: 1, b: 2 })).toEqual({
12+
1: "a",
13+
2: "b",
14+
});
15+
});
16+
17+
test("returns an empty object for empty input", () => {
18+
expect(invert({})).toEqual({});
19+
});
20+
});

0 commit comments

Comments
 (0)