|
5 | 5 | // Then it should swap the keys and values in the object |
6 | 6 | // E.g. invert({x : 10, y : 20}), target output: {"10": "x", "20": "y"} |
7 | 7 |
|
8 | | -// The original buggy implementation looked like this: |
9 | | -// |
| 8 | +// The original buggy implementation looked like this: |
| 9 | + |
10 | 10 | // function invert(obj) { |
11 | 11 | // const invertedObj = {}; |
12 | 12 | // for (const [key, value] of Object.entries(obj)) { |
13 | 13 | // invertedObj.key = value; |
14 | | -// invertedObj[value] = key; |
15 | 14 | // } |
16 | 15 | // return invertedObj; |
17 | 16 | // } |
18 | 17 |
|
19 | 18 | // a) What is the current return value when invert is called with { a: 1 }? |
20 | | -// -> { key: 1, "1": "a" } |
21 | | -// The line `invertedObj.key = value` sets a property literally named |
22 | | -// "key" (not the loop variable's value) to 1, alongside the correct |
23 | | -// inverted pair "1": "a" from the line below it. |
| 19 | +// -> { key: 1 } |
| 20 | +// Only invertedObj.key = value runs, setting a property literally |
| 21 | +// named "key" to 1. There is no swap line, so no "1": "a" pair |
| 22 | +// is ever added. |
24 | 23 |
|
25 | 24 | // b) What is the current return value when invert is called with { a: 1, b: 2 }? |
26 | | -// -> { key: 2, "1": "a", "2": "b" } |
27 | | -// Each loop iteration overwrites the same literal "key" property, so |
28 | | -// only the last value assigned to it survives - here, 2 from { b: 2 }. |
| 25 | +// -> { key: 2 } |
| 26 | +// Each loop iteration overwrites the same literal "key" property. |
| 27 | +// First a: key becomes 1, then b: key gets overwritten to 2. |
| 28 | +// No other properties are ever added, since there's no line that |
| 29 | +// writes to invertedObj[value]. |
| 30 | + |
| 31 | +// c) What is the target return value when invert is called with { a: 1, b: 2 }? |
| 32 | +// -> { "1": "a", "2": "b" } |
29 | 33 |
|
30 | 34 | // c) What does Object.entries return, and why is it needed? |
31 | 35 | // -> Object.entries(obj) returns an array of [key, value] pairs, e.g. |
|
35 | 39 | // destructuring ([key, value]) lets us pull out both parts at once. |
36 | 40 |
|
37 | 41 | // d) Why is the current return value different from the target output? |
38 | | -// -> The bug is `invertedObj.key = value`. Because "key" is written as a |
39 | | -// literal property name (dot notation), it always sets a property |
40 | | -// called "key" rather than using the value of the loop variable |
41 | | -// `key`. Only bracket notation - invertedObj[key] - would use the |
42 | | -// variable's actual value as the property name. This line also isn't |
43 | | -// needed at all for a correct inversion; it should be removed. |
44 | | - |
45 | | -// e) Fix: remove the incorrect `invertedObj.key = value` line entirely, |
46 | | -// leaving only `invertedObj[value] = key`, which correctly maps each |
47 | | -// value to its original key. See invert.test.js for tests proving |
48 | | -// the fix works, including empty objects and string values. |
| 42 | +// -> Two problems: (1) invertedObj.key uses dot notation with the |
| 43 | +// literal word "key", so it always writes to a property called |
| 44 | +// "key" rather than using the loop variable's value. (2) The line |
| 45 | +// that would actually perform the inversion - invertedObj[value] = key - |
| 46 | +// is missing entirely, so the object is never built up with the |
| 47 | +// swapped key/value pairs. |
| 48 | + |
| 49 | +// e) Fix: replace invertedObj.key = value with invertedObj[value] = key, |
| 50 | +// using bracket notation so the loop variable's value is used as the |
| 51 | +// property name. See invert.test.js for tests proving the fix works. |
49 | 52 | function invert(obj) { |
50 | 53 | const invertedObj = {}; |
51 | 54 | for (const [key, value] of Object.entries(obj)) { |
|
0 commit comments