File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1- // Let's define how invert should work
2-
3- // Given an object
4- // When invert is passed this object
5- // Then it should swap the keys and values in the object
6-
7- // E.g. invert({x : 10, y : 20}), target output: {"10": "x", "20": "y"}
8-
91function invert ( obj ) {
102 const invertedObj = { } ;
113
124 for ( const [ key , value ] of Object . entries ( obj ) ) {
13- invertedObj . key = value ;
5+ //invertedObj.key = value;
6+ invertedObj [ value ] = key ;
147 }
158
169 return invertedObj ;
1710}
1811
1912// a) What is the current return value when invert is called with { a : 1 }
20-
13+ /*
14+ {
15+ key: 1;
16+ }*/
2117// b) What is the current return value when invert is called with { a: 1, b: 2 }
22-
18+ /*
19+ {
20+ key: 2;
21+ }
22+ */
2323// c) What is the target return value when invert is called with {a : 1, b: 2}
24-
25- // c) What does Object.entries return? Why is it needed in this program?
26-
27- // d) Explain why the current return value is different from the target output
28-
29- // e) Fix the implementation of invert (and write tests to prove it's fixed!)
24+ // {"1":"a", "2":"b"}
25+ // d) What does Object.entries return? Why is it needed in this program?
26+ //object.entries takes an objecet and returns an array of its key-value pairs.
27+ // e) Explain why the current return value is different from the target output
28+ //because we use .key notation we are setting a property named "key" on our Object.
29+ // f) Fix the implementation of invert (and write tests to prove it's fixed!)
30+ module . exports = invert ;
Original file line number Diff line number Diff line change 1+ const invert = require ( "./invert.js" ) ;
2+
3+ test ( "swap key with value" , ( ) => {
4+ expect ( invert ( { a : 1 } ) ) . toEqual ( { 1 : "a" } ) ;
5+ } ) ;
6+
7+ test ( "swap key with value" , ( ) => {
8+ expect ( invert ( { a : 1 , b : 2 } ) ) . toEqual ( { 1 : "a" , 2 : "b" } ) ;
9+ } ) ;
You can’t perform that action at this time.
0 commit comments