Skip to content

Commit f998538

Browse files
Fix invert function to correctly swap keys and values in the object
1 parent 99caad7 commit f998538

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

Sprint-2/interpret/invert.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,26 @@ 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

1919
// a) What is the current return value when invert is called with { a : 1 }
20+
//["a",1]
2021

2122
// b) What is the current return value when invert is called with { a: 1, b: 2 }
23+
//[[ "a", 1], ["b", 2]]
2224

2325
// c) What is the target return value when invert is called with {a : 1, b: 2}
26+
//{"1":a,"2":b}
2427

2528
// c) What does Object.entries return? Why is it needed in this program?
29+
//returns an array so we can access each element of it so we can swap their order
2630

2731
// d) Explain why the current return value is different from the target output
32+
//it creates a new key:value pair with the key being "key",also it doesnt invert the key with the value
2833

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

0 commit comments

Comments
 (0)