Skip to content

Commit 05c6ac4

Browse files
author
Enice-Codes
committed
made corrections to file
1 parent bccf5d7 commit 05c6ac4

1 file changed

Lines changed: 24 additions & 21 deletions

File tree

‎Sprint-2/interpret/invert.js‎

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,31 @@
55
// Then it should swap the keys and values in the object
66
// E.g. invert({x : 10, y : 20}), target output: {"10": "x", "20": "y"}
77

8-
// The original buggy implementation looked like this:
9-
//
8+
// The original buggy implementation looked like this:
9+
1010
// function invert(obj) {
1111
// const invertedObj = {};
1212
// for (const [key, value] of Object.entries(obj)) {
1313
// invertedObj.key = value;
14-
// invertedObj[value] = key;
1514
// }
1615
// return invertedObj;
1716
// }
1817

1918
// 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.
2423

2524
// 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" }
2933

3034
// c) What does Object.entries return, and why is it needed?
3135
// -> Object.entries(obj) returns an array of [key, value] pairs, e.g.
@@ -35,17 +39,16 @@
3539
// destructuring ([key, value]) lets us pull out both parts at once.
3640

3741
// 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.
4952
function invert(obj) {
5053
const invertedObj = {};
5154
for (const [key, value] of Object.entries(obj)) {

0 commit comments

Comments
 (0)