File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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.key = value;
14+ invertedObj [ value ] = key ;
1415 }
1516
1617 return invertedObj ;
1718}
19+ /*
20+ console.log(invert({ a: 1 })); // {1:a}
21+ console.log(invert({ a: 1, b: 2 }));
22+ console.log(invert({ a: 1, b: 2 }));
23+ */
1824
1925// a) What is the current return value when invert is called with { a : 1 }
20-
26+ /*
27+ {
28+ key: 1;
29+ }
30+ */
2131// b) What is the current return value when invert is called with { a: 1, b: 2 }
22-
32+ /*
33+ {
34+ key: 2;
35+ }
36+ */
2337// c) What is the target return value when invert is called with {a : 1, b: 2}
24-
38+ // {"1":"a", "2":"b"}
2539// c) What does Object.entries return? Why is it needed in this program?
26-
40+ //object.entries takes an objecet and returns an array of its key-value pairs.
2741// d) Explain why the current return value is different from the target output
28-
42+ //because we use .key notation we are setting a property named "key" on our Object.
2943// e) Fix the implementation of invert (and write tests to prove it's fixed!)
44+
45+ module . exports = invert ;
Original file line number Diff line number Diff line change 1+ const invert = require ( "./invert.js" ) ;
2+ test ( "invert the key and values" , ( ) => {
3+ expect ( invert ( { a : 1 } ) ) . toEqual ( { 1 : "a" } ) ;
4+ } ) ;
5+
6+ test ( "invert the key and values" , ( ) => {
7+ expect ( invert ( { a : 1 , b : 2 } ) ) . toEqual ( { 1 : "a" , 2 : b } ) ;
8+ } ) ;
You can’t perform that action at this time.
0 commit comments