Skip to content

Commit e8054d3

Browse files
committed
Added tests and solved invert.js
1 parent c5baf81 commit e8054d3

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

Sprint-2/interpret/invert.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,28 @@ function invert(obj) {
1010
const invertedObj = {};
1111

1212
for (const [key, value] of Object.entries(obj)) {
13-
invertedObj.key = value;
13+
invertedObj[value] = key;
1414
}
1515

1616
return invertedObj;
1717
}
18+
module.exports = invert;
1819

1920
// a) What is the current return value when invert is called with { a : 1 }
21+
// I think its { key: 1 }
2022

2123
// b) What is the current return value when invert is called with { a: 1, b: 2 }
24+
// I think it should be { key: 2 }
2225

2326
// c) What is the target return value when invert is called with {a : 1, b: 2}
27+
// my suggestion is { '1': 'a', '2': 'b' }
2428

2529
// c) What does Object.entries return? Why is it needed in this program?
30+
// Object.entries returns an array of a given object's own enumerable string-keyed property [key, value] pairs.
31+
// It is needed in this program to iterate over the key-value pairs of the object to swap them.
2632

2733
// d) Explain why the current return value is different from the target output
34+
// I think its different because in the line invertedObj.key = value;
35+
// the key is being set as the string "key" instead of using the variable key.
2836

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

Sprint-2/interpret/invert.test.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
const invert = require("./invert.js");
2+
3+
describe("invert", () => {
4+
// Given an object with one key-value pair
5+
// When invert is called with this object
6+
// Then it should return an object with the key and value swapped
7+
test("invert swaps key and value for single pair", () => {
8+
expect(invert({ a: 1 })).toEqual({ 1: "a" });
9+
});
10+
11+
// Given an object with multiple key-value pairs
12+
// When invert is called with this object
13+
// Then it should return an object with the keys and values swapped
14+
test("invert swaps keys and values for multiple pairs", () => {
15+
expect(invert({ a: 1, b: 2 })).toEqual({ 1: "a", 2: "b" });
16+
});
17+
18+
// Given an object with string values
19+
// When invert is called with this object
20+
// Then it should return an object with the keys and values swapped
21+
test("invert swaps keys and values for string values", () => {
22+
expect(invert({ x: "apple", y: "banana" })).toEqual({
23+
apple: "x",
24+
banana: "y",
25+
});
26+
});
27+
28+
// Given an object with mixed value types
29+
// When invert is called with this object
30+
// Then it should return an object with the keys and values swapped
31+
test("invert swaps keys and values for mixed value types", () => {
32+
expect(invert({ a: 1, b: "two", c: 3 })).toEqual({
33+
1: "a",
34+
two: "b",
35+
3: "c",
36+
});
37+
});
38+
});

0 commit comments

Comments
 (0)