Skip to content

Commit b10547d

Browse files
committed
fix function, write tests
1 parent fbce304 commit b10547d

1 file changed

Lines changed: 22 additions & 2 deletions

File tree

‎Sprint-2/interpret/invert.js‎

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,40 @@ function invert(obj) {
1010
const invertedObj = {};
1111

1212
for (const [key, value] of Object.entries(obj)) {
13-
invertedObj.key = value;
13+
invertedObj[value] = key;
1414
}
15-
1615
return invertedObj;
1716
}
1817

1918
// a) What is the current return value when invert is called with { a : 1 }
19+
// the current return value is { key: 1 }
2020

2121
// b) What is the current return value when invert is called with { a: 1, b: 2 }
22+
// current return value is { key: 2 }
2223

2324
// c) What is the target return value when invert is called with {a : 1, b: 2}
25+
// the target return value is { "1": "a", "2": "b" }
2426

2527
// c) What does Object.entries return? Why is it needed in this program?
28+
// Object.entries returns an array of key-value pairs from the object. It is needed
29+
// in this program to iterate over each key-value pair in the object so that we can
30+
// swap them and create a new inverted object.
2631

2732
// d) Explain why the current return value is different from the target output
33+
// I think current return value is different from the return value because the
34+
// current implementation is not really swapping the properties of the object.
35+
// Instead, it is just creating a new property called "key" and assigning the
36+
// value to it. The target output requires us to swap the keys and values, which
37+
// is not happening in the current implementation.
2838

2939
// e) Fix the implementation of invert (and write tests to prove it's fixed!)
40+
console.assert(
41+
JSON.stringify(invert({ a: 1 })) === JSON.stringify({ "1": "a" }),
42+
"Test 1 failed"
43+
);
44+
45+
console.assert(
46+
JSON.stringify(invert({ a: 1, b: 2 })) ===
47+
JSON.stringify({ "1": "a", "2": "b" }),
48+
"Test 2 failed"
49+
);

0 commit comments

Comments
 (0)