|
7 | 7 | // E.g. invert({x : 10, y : 20}), target output: {"10": "x", "20": "y"} |
8 | 8 |
|
9 | 9 | function invert(obj) { |
| 10 | + // need obj == null because 'type of obj === null' does not work in Js |
| 11 | + if (typeof obj !== "object" || obj === null || Array.isArray(obj)) { |
| 12 | + throw new TypeError("Input must be a plain object"); |
| 13 | + } |
| 14 | + |
10 | 15 | const invertedObj = {}; |
11 | 16 |
|
12 | 17 | for (const [key, value] of Object.entries(obj)) { |
13 | | - invertedObj.key = value; |
| 18 | + // if a value is not a number or string, it can't be used as a key in the inverted version |
| 19 | + if (typeof value !== "string" && typeof value !== "number") { |
| 20 | + throw new TypeError(`Value ${JSON.stringify(value)} is not a valid key`); |
| 21 | + } |
| 22 | + invertedObj[String(value)] = String(key); |
14 | 23 | } |
15 | 24 |
|
16 | 25 | return invertedObj; |
17 | 26 | } |
18 | 27 |
|
| 28 | +module.exports = invert; |
19 | 29 | // a) What is the current return value when invert is called with { a : 1 } |
| 30 | +/* |
| 31 | +It would return {key: 1}. The dot nation means that it uses the string "key" as the key. |
| 32 | +and the value for that key is the value from the object being iterated over. |
| 33 | +*/ |
20 | 34 |
|
21 | 35 | // b) What is the current return value when invert is called with { a: 1, b: 2 } |
| 36 | +/* |
| 37 | +It would return {key : 2}. First it would do as in a, and the overwrite the value of the key "key" |
| 38 | +from 1 to 2. |
| 39 | +*/ |
22 | 40 |
|
23 | 41 | // c) What is the target return value when invert is called with {a : 1, b: 2} |
| 42 | +/* |
| 43 | +The target is {"1": "a", "2": "b"} |
| 44 | +*/ |
24 | 45 |
|
25 | 46 | // c) What does Object.entries return? Why is it needed in this program? |
| 47 | +/* |
| 48 | +It returns a 2d array containing arrays of key value pairs from the object of the form: |
| 49 | +[[key1, value1], [key2, value2], [key3, value3]]. |
| 50 | +
|
| 51 | +It is needed because can't iterate over objects the same way as arrays. Objects are a bit more |
| 52 | +complicated, because you have to decide if you want to iterate over the keys, values, or both. |
| 53 | +you also have to decide if you want to include inherited properties. |
26 | 54 |
|
| 55 | +Object.entries() makes this assumption for you, by including only the object's own properties. |
| 56 | +It also just makes this particular task easier by accessing the key and value together. |
| 57 | +If you wanted to include inherited properties you'd use (for ... in) loop. |
| 58 | +
|
| 59 | +*/ |
27 | 60 | // d) Explain why the current return value is different from the target output |
| 61 | +/* |
| 62 | +The dot nation means that it uses the string "key" as the key. |
| 63 | +and the value for that key is the value from the object being iterated over. |
| 64 | +So it just continuously overwrites that one "key" property of the to-be-returned object. |
| 65 | +
|
| 66 | +It's also swapping key, value from the old object. |
| 67 | +*/ |
28 | 68 |
|
29 | 69 | // e) Fix the implementation of invert (and write tests to prove it's fixed!) |
0 commit comments