Skip to content

Commit ddd29d3

Browse files
committed
Fix: Added missing module.exports to invert
1 parent 86ed817 commit ddd29d3

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

Sprint-2/interpret/invert.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,25 @@
77
// E.g. invert({x : 10, y : 20}), target output: {"10": "x", "20": "y"}
88

99
function invert(obj) {
10+
// Check if the input is not an object
11+
if (typeof obj !== "object" || obj === null || Array.isArray(obj)) {
12+
throw new Error("Input must be an object");
13+
}
14+
1015
const invertedObj = {};
1116

17+
// Loop through each key value pair and swap them
1218
for (const [key, value] of Object.entries(obj)) {
13-
invertedObj.key = value;
19+
// bracket notation uses the variable,
20+
// not the literal string "key"
21+
invertedObj[value] = key;
1422
}
1523

1624
return invertedObj;
1725
}
1826

27+
module.exports = invert;
28+
1929
// a) What is the current return value when invert is called with { a : 1 }
2030
// The current return value when invert is called with { a : 1 } is { key:1 }
2131

0 commit comments

Comments
 (0)