Skip to content

Commit 75ce6cb

Browse files
committed
Fix invert function to correctly swap keys and values, add input validation for non-object types
1 parent 1c17fbf commit 75ce6cb

1 file changed

Lines changed: 41 additions & 4 deletions

File tree

‎Sprint-2/interpret/invert.js‎

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// Then it should swap the keys and values in the object
66

77
// E.g. invert({x : 10, y : 20}), target output: {"10": "x", "20": "y"}
8+
/*
89
910
function invert(obj) {
1011
const invertedObj = {};
@@ -15,15 +16,51 @@ function invert(obj) {
1516
1617
return invertedObj;
1718
}
19+
*/
20+
/* a) What is the current return value when invert is called with { a : 1 }
1821
19-
// a) What is the current return value when invert is called with { a : 1 }
22+
{ key: 1 }
2023
2124
// b) What is the current return value when invert is called with { a: 1, b: 2 }
2225
26+
{ key: 2 }
2327
// c) What is the target return value when invert is called with {a : 1, b: 2}
2428
25-
// c) What does Object.entries return? Why is it needed in this program?
29+
{ "1": "a", "2": "b" }
30+
// d) What does Object.entries return? Why is it needed in this program?
2631
27-
// d) Explain why the current return value is different from the target output
32+
Object.entries(obj) returns an array of an object's own key-value pairs as [key, value] tuples (e.g., Object.entries({ a: 1, b: 2 }) returns [['a', 1], ['b', 2]]).
2833
29-
// e) Fix the implementation of invert (and write tests to prove it's fixed!)
34+
It is needed here so we can loop over both the property name (key) and its corresponding value (value) at the same time using array destructuring (const [key, value]).
35+
36+
// e) Explain why the current return value is different from the target output
37+
38+
There are two critical issues in the current loop
39+
1. Static property assignment instead of dynamic lookup;
40+
The code writes invertedObj.key = value. Using dot notation literally creates/overwrites a single key named "key" on the object every single iteration.
41+
To use the variable's value dynamically as the object key, you must use bracket notation: invertedObj[value].
42+
43+
2. Flipped key and value;
44+
The target output asks to swap keys and values (so values become keys and keys become values). The original code assigns value to the object's key position,
45+
rather than assigning key to the position indexed by value (invertedObj[value] = key). */
46+
47+
// f) Fix the implementation of invert (and write tests to prove it's fixed!)
48+
49+
function invert(obj) {
50+
// Guard clause for non-object inputs
51+
if (typeof obj !== "object" || obj === null || Array.isArray(obj)) {
52+
return {};
53+
}
54+
55+
const invertedObj = {};
56+
57+
for (const [key, value] of Object.entries(obj)) {
58+
// Use bracket notation to dynamically set the property name to 'value'
59+
// and assign 'key' as its value
60+
invertedObj[value] = key;
61+
}
62+
63+
return invertedObj;
64+
}
65+
66+
module.exports = invert;

0 commit comments

Comments
 (0)