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,29 @@ function invert(obj) {
1010 const invertedObj = { } ;
1111
1212 for ( const [ key , value ] of Object . entries ( obj ) ) {
13- invertedObj . key = value ;
13+
14+ invertedObj [ value ] = key ;
1415 }
1516
1617 return invertedObj ;
1718}
1819
19- // a) What is the current return value when invert is called with { a : 1 }
20+ console . log ( invert ( { a :1 , b :2 , apple :4 } ) )
21+
22+ // a) What is the current return value when invert is called with { a : 1 }
23+ // it returns {key:1}
2024
2125// b) What is the current return value when invert is called with { a: 1, b: 2 }
26+ // it returns {key:2}
2227
2328// c) What is the target return value when invert is called with {a : 1, b: 2}
29+ // the target return value should be {'1': 'a','2':'b'}
2430
2531// c) What does Object.entries return? Why is it needed in this program?
32+ // Object.entries(obj) lets loop [key, value] pairs easily because .entries(object/array) is built in method in javascript
2633
2734// d) Explain why the current return value is different from the target output
35+ // Because invertedObj.key=value is wrong which means key of property will be always 'key' and value will remains the same as obj array in invertedObj
2836
2937// e) Fix the implementation of invert (and write tests to prove it's fixed!)
38+ // I put invertedObh[value]= key instead then the function works as expected
You can’t perform that action at this time.
0 commit comments