You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Sprint-2/interpret/invert.js
+11-1Lines changed: 11 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -10,20 +10,30 @@ function invert(obj) {
10
10
constinvertedObj={};
11
11
12
12
for(const[key,value]ofObject.entries(obj)){
13
-
invertedObj.key=value;
13
+
invertedObj[value]=key;
14
14
}
15
15
16
16
returninvertedObj;
17
17
}
18
18
19
+
19
20
// 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}
20
22
21
23
// 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}
22
25
23
26
// 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}
24
28
25
29
// 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:)
26
32
27
33
// 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}
28
35
29
36
// e) Fix the implementation of invert (and write tests to prove it's fixed!)
0 commit comments