Skip to content

Commit 79e9ce6

Browse files
committed
Implement: invert function and test cases
1 parent 72f6c7a commit 79e9ce6

File tree

2 files changed

+88
-1
lines changed

2 files changed

+88
-1
lines changed

Sprint-2/interpret/invert.js

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,63 @@
77
// E.g. invert({x : 10, y : 20}), target output: {"10": "x", "20": "y"}
88

99
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+
1015
const invertedObj = {};
1116

1217
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);
1423
}
1524

1625
return invertedObj;
1726
}
1827

28+
module.exports = invert;
1929
// 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+
*/
2034

2135
// 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+
*/
2240

2341
// 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+
*/
2445

2546
// 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.
2654
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+
*/
2760
// 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+
*/
2868

2969
// e) Fix the implementation of invert (and write tests to prove it's fixed!)

Sprint-2/interpret/invert.test.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
const invert = require("./invert.js");
2+
3+
test("Inverts simple object with string key and string value", () => {
4+
expect(invert({ a: "x", b: "y", c: "z" })).toEqual({
5+
x: "a",
6+
y: "b",
7+
z: "c",
8+
});
9+
});
10+
11+
test("Returns empty object if input is an empty object", () => {
12+
expect(invert({})).toEqual({});
13+
});
14+
15+
test("Throws error for non-object inputs", () => {
16+
expect(() => invert("asdf")).toThrow(TypeError);
17+
expect(() => invert([1, "a", 2, "b"])).toThrow(TypeError);
18+
expect(() => invert()).toThrow();
19+
expect(() => invert(null)).toThrow();
20+
});
21+
22+
test("Throws errors for non-string, non-numeric keys or values", () => {
23+
expect(() => invert({ a: { b: 12 } })).toThrow(TypeError);
24+
});
25+
26+
test("Duplicate values become one key, where the last key in original becomes its new value", () => {
27+
expect(invert({ a: "x", b: "x" })).toEqual({ x: "b" });
28+
});
29+
30+
test("numeric keys become string converted to value", () => {
31+
expect(invert({ 1: "a", 2: "b" })).toEqual({ a: "1", b: "2" });
32+
});
33+
34+
test("invert twice returns original if all values are unique", () => {
35+
expect(invert(invert({ a: "x", b: "y", x: "z" }))).toEqual({
36+
a: "x",
37+
b: "y",
38+
x: "z",
39+
});
40+
});
41+
42+
test("invert twice does not returns original if all values are not unique", () => {
43+
expect(invert(invert({ a: "x", b: "y", c: "y" }))).toEqual({
44+
a: "x",
45+
c: "y",
46+
});
47+
});

0 commit comments

Comments
 (0)