Skip to content

Commit 7e70409

Browse files
complete-interpret-exercises
1 parent f0bcfcd commit 7e70409

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

Sprint-2/interpret/invert.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,30 @@ 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+
1920
// a) What is the current return value when invert is called with { a : 1 }
21+
// invertedObj.key = value; this line creates a key literally with name "key". it returns {key : 1}
2022

2123
// b) What is the current return value when invert is called with { a: 1, b: 2 }
24+
// every time it meets the key and reassign it, so {key : 2}
2225

2326
// c) What is the target return value when invert is called with {a : 1, b: 2}
27+
// to swap pair key-value with each other. should return {1 : a, 2: b}
2428

2529
// c) What does Object.entries return? Why is it needed in this program?
30+
// it returns key-value pairs in arrays nested of array. we need Object.entries to convert key-value pairs to array because for..of doesn't work with objects.
31+
//I wonder if we could solve the function by swapping key-value using [key, value] = [value, key] in some way? p.s. I'll delete this comment after review:)
2632

2733
// d) Explain why the current return value is different from the target output
34+
// invertedObj.key = value; this line creates a key literally with name "key". it returns {key : 1}
2835

2936
// e) Fix the implementation of invert (and write tests to prove it's fixed!)
37+
38+
39+
module.exports = invert;

Sprint-2/interpret/invert.test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const invert = require("./invert.js");
2+
3+
4+
test(" should return an object with swapped keys and values", () => {
5+
expect(invert({"a" : "1", "b": "2"})).toEqual({"1" : "a", "2" : "b"});
6+
});

0 commit comments

Comments
 (0)