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,33 @@ 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+ console . log ( invert ( { a : 1 } ) ) ;
20+ console . log ( invert ( { a : 1 , b : 2 } ) ) ;
21+ console . log ( invert ( { a : 1 , b : 2 } ) ) ;
1822
19- // a) What is the current return value when invert is called with { a : 1 }
23+ module . exports = invert ;
2024
25+ // a) What is the current return value when invert is called with { a : 1 }
26+ /*{
27+ key: 1;
28+ }
29+ */
2130// b) What is the current return value when invert is called with { a: 1, b: 2 }
22-
31+ /*
32+ {
33+ key: 2;
34+ }
35+ */
2336// c) What is the target return value when invert is called with {a : 1, b: 2}
24-
37+ // {"1":"a", "2":"b"}
2538// c) What does Object.entries return? Why is it needed in this program?
26-
39+ //object.entries takes an objecet and returns an array of its key-value pairs.
2740// d) Explain why the current return value is different from the target output
28-
41+ //because we use .key notation we are setting a property named "key" on our Object.
2942// e) Fix the implementation of invert (and write tests to prove it's fixed!)
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