Skip to content

Commit 1cc69af

Browse files
committed
Fixed the code.
1 parent da2a8c5 commit 1cc69af

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

Sprint-2/interpret/invert.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,25 @@
66

77
// E.g. invert({x : 10, y : 20}), target output: {"10": "x", "20": "y"}
88

9+
// function invert(obj) {
10+
// const invertedObj = {};
11+
12+
// for (const [key, value] of Object.entries(obj)) {
13+
// invertedObj[value] = key;
14+
// }
15+
16+
// return invertedObj;
17+
// }
918
function invert(obj) {
1019
const invertedObj = {};
1120

12-
for (const [key, value] of Object.entries(obj)) {
13-
invertedObj[value] = key;
21+
for (const key in obj) {
22+
invertedObj[obj[key]] = key; // <-- this is correct
1423
}
1524

1625
return invertedObj;
1726
}
27+
1828
console.log(invert({ a: 1 })); // {1: "a"}
1929
console.log(invert({ a: 1, b: 2 })); // {1: "a", 2: "b"}
2030
module.exports = invert;

0 commit comments

Comments
 (0)