File tree Expand file tree Collapse file tree 1 file changed +11
-1
lines changed
Expand file tree Collapse file tree 1 file changed +11
-1
lines changed Original file line number Diff line number Diff line change 77// E.g. invert({x : 10, y : 20}), target output: {"10": "x", "20": "y"}
88
99function invert ( obj ) {
10+ // Check if the input is not an object
11+ if ( typeof obj !== "object" || obj === null || Array . isArray ( obj ) ) {
12+ throw new Error ( "Input must be an object" ) ;
13+ }
14+
1015 const invertedObj = { } ;
1116
17+ // Loop through each key value pair and swap them
1218 for ( const [ key , value ] of Object . entries ( obj ) ) {
13- invertedObj . key = value ;
19+ // bracket notation uses the variable,
20+ // not the literal string "key"
21+ invertedObj [ value ] = key ;
1422 }
1523
1624 return invertedObj ;
1725}
1826
27+ module . exports = invert ;
28+
1929// a) What is the current return value when invert is called with { a : 1 }
2030// The current return value when invert is called with { a : 1 } is { key:1 }
2131
You can’t perform that action at this time.
0 commit comments