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
{{ message }}
Repository navigation
Commit 03c6296
Browse filesBrowse the repository at this point in the historyBrowse files
Copy file name to clipboardExpand all lines: Sprint-2/interpret/invert.js
+21-5Lines changed: 21 additions & 5 deletions
Original file line number
Diff line number
Diff line change
@@ -5,7 +5,7 @@
5
5
// Then it should swap the keys and values in the object
6
6
7
7
// E.g. invert({x : 10, y : 20}), target output: {"10": "x", "20": "y"}
8
-
8
+
/*
9
9
function invert(obj) {
10
10
const invertedObj = {};
11
11
@@ -15,15 +15,31 @@ function invert(obj) {
15
15
16
16
return invertedObj;
17
17
}
18
+
*/
18
19
19
-
// a) What is the current return value when invert is called with { a : 1 }
20
+
// a) What is the current return value when invert is called with { a : 1 } - The current return value is { key: 1}
20
21
21
-
// b) What is the current return value when invert is called with { a: 1, b: 2 }
22
+
// b) What is the current return value when invert is called with { a: 1, b: 2 } - The current return value is { key: 2}
22
23
23
-
// c) What is the target return value when invert is called with {a : 1, b: 2}
24
+
// c) What is the target return value when invert is called with {a : 1, b: 2} - The target return value is { "1": "a", "2": "b" }
24
25
25
26
// c) What does Object.entries return? Why is it needed in this program?
27
+
/* Object.entries(obj) returns an array of [key, value] pairs. Example: Object.entries({ a:1, b:2 }) returns [["a", 1], ["b", 2]].
28
+
It is needed because the loop "for (const [key, value] of Object.entries(obj))" let's each pair destructure easily.
29
+
*/
26
30
27
-
// d) Explain why the current return value is different from the target output
31
+
// d) Explain why the current return value is different from the target output - It is because this line "invertedObj.key = value;" uses literal string "key instead of the variable key" which is a wrong property name,
32
+
// instead of this "invertedObj[value] = key;"
28
33
29
34
// e) Fix the implementation of invert (and write tests to prove it's fixed!)
0 commit comments