Skip to content

Commit 4de66ef

Browse files
committed
implement function and answer Qs in the comments
1 parent 95e7d98 commit 4de66ef

1 file changed

Lines changed: 11 additions & 2 deletions

File tree

‎Sprint-2/interpret/invert.js‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,29 @@ 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+
console.log(invert({ a: 1 }));
20+
//return value is { key : 1 }
2021

2122
// b) What is the current return value when invert is called with { a: 1, b: 2 }
23+
console.log(invert({ a: 1, b: 2 }));
24+
//return value is { key : 2 }
2225

2326
// c) What is the target return value when invert is called with {a : 1, b: 2}
27+
// target return value: {"1" : "a", "2": "b"}
2428

2529
// c) What does Object.entries return? Why is it needed in this program?
30+
//each key-value pair will form an array in a bigger array (array of arrays of key-value pairs)
2631

2732
// d) Explain why the current return value is different from the target output
33+
/* issues:
34+
- .key notation naming the key "key"
35+
- we only assign the key to value, but not reversing it
36+
*/
2837

2938
// e) Fix the implementation of invert (and write tests to prove it's fixed!)

0 commit comments

Comments
 (0)