Skip to content

Commit 4c91d5d

Browse files
authored
Correct key-value inversion in invert function
Fix the implementation of the invert function to correctly swap keys and values.
1 parent f1acb21 commit 4c91d5d

1 file changed

Lines changed: 18 additions & 7 deletions

File tree

‎Sprint-2/interpret/invert.js‎

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,31 @@ 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
}
1515

1616
return invertedObj;
1717
}
1818

19-
// a) What is the current return value when invert is called with { a : 1 }
19+
console.log("invert.js loaded");
2020

21-
// b) What is the current return value when invert is called with { a: 1, b: 2 }
21+
module.exports = invert;
2222

23-
// c) What is the target return value when invert is called with {a : 1, b: 2}
23+
// a) What is the current return value when invert is called with {a : 1}?
24+
// The current return value when invert is called with {a: 1} is {key: 1}.
2425

25-
// c) What does Object.entries return? Why is it needed in this program?
26+
// b) What is the current return value when invert is called with {a: 1, b: 2}?
27+
// The current return value when invert is called with {a: 1, b: 2} is {key: 2}.
2628

27-
// d) Explain why the current return value is different from the target output
29+
// c) What is the target return value when invert is called with {a: 1, b: 2}?
30+
// The target return value when invert is called with {a: 1, b: 2} is {1: a, 2: b}.
2831

29-
// e) Fix the implementation of invert (and write tests to prove it's fixed!)
32+
// d) What does Object.entries return? Why is it needed in this program?
33+
// Object.entries() returns an array containing the key-value pairs of an object.
34+
// It is needed in this program because the function must access both the keys and the values in order to swap them.
35+
36+
// e) Explain why the current return value is different from the target output.
37+
// The current return value is different from the target output because invertedObj.key = value does not use the variable key as the property name.
38+
// Bracket notation is needed to swap the keys and values.
39+
40+
// f) Fix the implementation of invert (and write tests to prove it's fixed!)

0 commit comments

Comments
 (0)