Skip to content

Commit 0357dec

Browse files
committed
invert test and implement
1 parent 8730b63 commit 0357dec

1 file changed

Lines changed: 17 additions & 1 deletion

File tree

Sprint-2/interpret/invert.js

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

19+
module.exports = invert;
20+
1921
// a) What is the current return value when invert is called with { a : 1 }
22+
// { key: 1 }
2023

2124
// b) What is the current return value when invert is called with { a: 1, b: 2 }
25+
//{ key: 2 }
2226

2327
// c) What is the target return value when invert is called with {a : 1, b: 2}
28+
//{
29+
// 1: "a",
30+
// 2: "b"
31+
//}
2432

2533
// c) What does Object.entries return? Why is it needed in this program?
34+
//
35+
//[
36+
// ["a", 1],
37+
//["b", 2]
38+
//]
2639

2740
// d) Explain why the current return value is different from the target output
41+
//invertedObj.key = value; means "create a property named key".
42+
//It does not mean "use the variable key as the property name."
43+
//invertedObj[value] = key; Square brackets allow the variable's value to become the property name.
2844

2945
// e) Fix the implementation of invert (and write tests to prove it's fixed!)

0 commit comments

Comments
 (0)