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
+19-5Lines changed: 19 additions & 5 deletions
Original file line number
Diff line number
Diff line change
@@ -10,20 +10,34 @@ 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
// a) What is the current return value when invert is called with { a : 1 }
20
-
20
+
//{ key: 1 }
21
21
// b) What is the current return value when invert is called with { a: 1, b: 2 }
22
-
22
+
//{ key: 2 }
23
23
// c) What is the target return value when invert is called with {a : 1, b: 2}
24
+
//{"1": "a", "2": "b"}
24
25
25
26
// c) What does Object.entries return? Why is it needed in this program?
26
-
27
+
// Object.entries gives an array of [key, value] pairs.
27
28
// d) Explain why the current return value is different from the target output
28
-
29
+
// invertedObj.key sets a property literally called "key". We need to use the variable key as the dynamic property name. and we need to change "key" to "value" and "value" to "key".
29
30
// e) Fix the implementation of invert (and write tests to prove it's fixed!)
0 commit comments