|
4 | 4 | // When invert is passed this object |
5 | 5 | // Then it should swap the keys and values in the object |
6 | 6 |
|
7 | | -// E.g. invert({x : 10, y : 20}), target output: {"10": "x", "20": "y"} |
| 7 | +// Example: |
| 8 | +// invert({ x: 10, y: 20 }) |
| 9 | +// should return: |
| 10 | +// { "10": "x", "20": "y" } |
8 | 11 |
|
9 | | -function invert(obj) { |
10 | | - const invertedObj = {}; |
| 12 | +/* |
| 13 | +a) What is the current return value when invert({ a: 1 }) is called? |
11 | 14 |
|
12 | | - for (const [key, value] of Object.entries(obj)) { |
13 | | - invertedObj.key = value; |
14 | | - } |
| 15 | +Current output: |
| 16 | +{ key: 1 } |
15 | 17 |
|
16 | | - return invertedObj; |
| 18 | +Reason: |
| 19 | +The code uses invertedObj.key which creates a property literally called "key" |
| 20 | +instead of using the variable key. |
| 21 | +
|
| 22 | +--------------------------------------------------- |
| 23 | +
|
| 24 | +b) What is the current return value when invert({ a: 1, b: 2 }) is called? |
| 25 | +
|
| 26 | +Current output: |
| 27 | +{ key: 2 } |
| 28 | +
|
| 29 | +Reason: |
| 30 | +The second iteration overwrites the first value because the property name |
| 31 | +is always "key". |
| 32 | +
|
| 33 | +--------------------------------------------------- |
| 34 | +
|
| 35 | +c) What is the target return value when invert({ a: 1, b: 2 }) is called? |
| 36 | +
|
| 37 | +Expected output: |
| 38 | +{ |
| 39 | + "1": "a", |
| 40 | + "2": "b" |
17 | 41 | } |
18 | 42 |
|
19 | | -// a) What is the current return value when invert is called with { a : 1 } |
| 43 | +Keys and values should be swapped. |
| 44 | +
|
| 45 | +--------------------------------------------------- |
20 | 46 |
|
21 | | -// b) What is the current return value when invert is called with { a: 1, b: 2 } |
| 47 | +d) What does Object.entries return? Why is it needed? |
22 | 48 |
|
23 | | -// c) What is the target return value when invert is called with {a : 1, b: 2} |
| 49 | +Object.entries(obj) converts an object into an array of [key, value] pairs. |
24 | 50 |
|
25 | | -// c) What does Object.entries return? Why is it needed in this program? |
| 51 | +Example: |
| 52 | +Object.entries({ a: 1, b: 2 }) |
26 | 53 |
|
27 | | -// d) Explain why the current return value is different from the target output |
| 54 | +returns: |
| 55 | +[ |
| 56 | + ["a", 1], |
| 57 | + ["b", 2] |
| 58 | +] |
| 59 | +
|
| 60 | +This allows us to loop through object properties using for...of. |
| 61 | +
|
| 62 | +--------------------------------------------------- |
| 63 | +
|
| 64 | +e) Why is the current return value different from the target output? |
| 65 | +
|
| 66 | +Because invertedObj.key uses the literal property name "key". |
| 67 | +To use the value dynamically as the key, we must use bracket notation: |
| 68 | +invertedObj[value] = key; |
| 69 | +*/ |
| 70 | + |
| 71 | +function invert(obj) { |
| 72 | + const invertedObj = {}; |
| 73 | + |
| 74 | + for (const [key, value] of Object.entries(obj)) { |
| 75 | + // Correct implementation: swap key and value |
| 76 | + invertedObj[value] = key; |
| 77 | + } |
| 78 | + |
| 79 | + return invertedObj; |
| 80 | +} |
28 | 81 |
|
29 | | -// e) Fix the implementation of invert (and write tests to prove it's fixed!) |
| 82 | +module.exports = invert; |
0 commit comments