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
+15-2Lines changed: 15 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -10,20 +10,33 @@ 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
+
console.log(invert({a: 1}));
19
+
console.log(invert({a: 1,b: 2}));
20
+
console.log(invert({a: 1,b: 2}));
18
21
19
22
// a) What is the current return value when invert is called with { a : 1 }
23
+
// the current output value of this invert is {key:1}.
20
24
21
25
// b) What is the current return value when invert is called with { a: 1, b: 2 }
26
+
/* the current output value of this invert is {key:2} because
27
+
instead of accessing the [key] variable inside the condition parameter for store key for each loop, the code in line 13 instead adding .key behind and the system
28
+
interpret as string literal so each loop is overwrite the previous key value.
29
+
.*/
22
30
23
31
// c) What is the target return value when invert is called with {a : 1, b: 2}
32
+
// is also the same with { a: 1, b: 2 } because is overwrite the previous key each loop.
24
33
25
34
// c) What does Object.entries return? Why is it needed in this program?
35
+
/* Object.entries return deconstruct object key and value pair into individual item in an array.
36
+
This is very important because since object is are not iterable, this help by deconstructing object into array when we try to loop each key and value pair*/
26
37
27
38
// d) Explain why the current return value is different from the target output
28
-
39
+
/*
40
+
Because inside the for of loop, the invertedObj output assign they keys first instead of value for each loop.
41
+
*/
29
42
// e) Fix the implementation of invert (and write tests to prove it's fixed!)
0 commit comments