Skip to content

Commit b64fa9b

Browse files
committed
invert.js invert.test.js committed
1 parent f82e576 commit b64fa9b

File tree

2 files changed

+85
-1
lines changed

2 files changed

+85
-1
lines changed

Sprint-2/interpret/invert.js

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/**
12
// Let's define how invert should work
23
34
// Given an object
@@ -15,15 +16,48 @@ function invert(obj) {
1516
1617
return invertedObj;
1718
}
19+
*/
1820

1921
// a) What is the current return value when invert is called with { a : 1 }
22+
/**
23+
* { key: 1 }
24+
* Because invertedObj.key = value sets a property literally named "key" to the value.
25+
*/
2026

2127
// b) What is the current return value when invert is called with { a: 1, b: 2 }
28+
/**
29+
* { key: 2 }
30+
* The loop runs twice, but each time it overwrites the "key" property. However, the last value (2) remains.
31+
*/
2232

2333
// c) What is the target return value when invert is called with {a : 1, b: 2}
34+
/**
35+
* { "1": "a", "2": "b" }
36+
*/
2437

25-
// c) What does Object.entries return? Why is it needed in this program?
38+
// d) What does Object.entries return? Why is it needed in this program?
39+
/**
40+
* Object.entries() returns an array of a given object's enumerable string-keyed property [key, value] pairs. For example:
41+
* Object.entries({ a: 1, b: 2 }) // returns [['a', 1], ['b', 2]]
42+
*/
2643

2744
// d) Explain why the current return value is different from the target output
45+
/**
46+
* The current implementation uses invertedObj.key = value which creates a property with the literal string "key" rather than using the variable key as the property name. To use the variable's value as the property name, we need bracket notation: invertedObj[value] = key.
47+
*/
2848

2949
// e) Fix the implementation of invert (and write tests to prove it's fixed!)
50+
51+
function invert(obj) {
52+
const invertedObj = {};
53+
54+
for (const [key, value] of Object.entries(obj)) {
55+
// Use bracket notation to set the property name to the value
56+
// and the property value to the original key
57+
invertedObj[value] = key;
58+
}
59+
60+
return invertedObj;
61+
}
62+
63+
module.exports = invert;

Sprint-2/interpret/invert.test.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
const invert = require("./invert.js");
2+
3+
describe("invert function", () => {
4+
test("inverts a simple object with one key-value pair", () => {
5+
expect(invert({ a: 1 })).toEqual({ "1": "a" });
6+
});
7+
8+
test("inverts an object with multiple key-value pairs", () => {
9+
expect(invert({ a: 1, b: 2 })).toEqual({ "1": "a", "2": "b" });
10+
});
11+
12+
test("inverts an object with string values", () => {
13+
expect(invert({ name: "John", city: "New York" })).toEqual({
14+
"John": "name",
15+
"New York": "city"
16+
});
17+
});
18+
19+
test("inverts an object with mixed value types", () => {
20+
expect(invert({ x: 10, y: "hello", z: true })).toEqual({
21+
"10": "x",
22+
"hello": "y",
23+
"true": "z"
24+
});
25+
});
26+
27+
test("handles empty object", () => {
28+
expect(invert({})).toEqual({});
29+
});
30+
31+
test("handles object with duplicate values (later ones overwrite)", () => {
32+
expect(invert({ a: 1, b: 1 })).toEqual({ "1": "b" });
33+
});
34+
35+
test("handles object with numeric keys", () => {
36+
const result = invert({ 1: "one", 2: "two" });
37+
expect(result).toEqual({ one: "1", two: "2" });
38+
});
39+
40+
test("preserves all properties", () => {
41+
const original = { color: "red", size: "large", price: 100 };
42+
const inverted = invert(original);
43+
44+
// Check that all original values become keys in the inverted object
45+
// Sort the arrays to compare regardless of order (numeric keys are sorted differently)
46+
expect(Object.keys(inverted).sort()).toEqual(["red", "large", "100"].sort());
47+
// Check that inverted values are the original keys
48+
expect(Object.values(inverted).sort()).toEqual(["color", "size", "price"].sort());
49+
});
50+
});

0 commit comments

Comments
 (0)