Skip to content

Commit 62636b1

Browse files
committed
ivert function and testing it
1 parent c27db66 commit 62636b1

File tree

2 files changed

+88
-1
lines changed

2 files changed

+88
-1
lines changed

Sprint-2/interpret/invert.js

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,49 @@ 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
}
1818

19+
const profile = {
20+
name: "Alex",
21+
age: 19,
22+
occupation: "student",
23+
};
24+
25+
//console.log(Object.entries(profile));
26+
//const result = invert(profile);
27+
//console.log(result);
28+
29+
// console.log(invert({ a: 1, b: 2 }));
30+
1931
// a) What is the current return value when invert is called with { a : 1 }
32+
// {key: 1}
2033

2134
// b) What is the current return value when invert is called with { a: 1, b: 2 }
35+
// { key: 2 }
2236

2337
// c) What is the target return value when invert is called with {a : 1, b: 2}
38+
// {1 : a, 2: b}
2439

2540
// c) What does Object.entries return? Why is it needed in this program?
41+
// It returns e.g. [ [ 'name', 'Alex' ], [ 'age', 19 ], [ 'occupation', 'student' ] ] array of arrays
42+
// It is needed because we want to iterate through an array to get key and values pars for new object.
2643

2744
// d) Explain why the current return value is different from the target output
45+
// The current return value is different from the target output because the function
46+
// is working improperly. First of all, the key should be taken on brackets to make possible
47+
// to read every key name-value and value in every pair in subarray.
48+
// invertedObj.key = value: This is a direct assignment. It treats "key" as a literal string
49+
// and creates or updates a property named "key" on the invertedObj object, setting its value
50+
// to the value of the value variable.
51+
// invertedObj[key] = value: This uses bracket notation. It evaluates the key variable and uses
52+
// its value (which is a string) as the property name on invertedObj.
53+
// Secondly, change the key and value places for new object creation statement
54+
// 'invertedObj[value] = key;' to get swapping the keys and values in the new object.
2855

2956
// e) Fix the implementation of invert (and write tests to prove it's fixed!)
57+
58+
module.exports = invert;

Sprint-2/interpret/invert.test.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
const invert = require("./invert.js");
2+
3+
describe("edge cases for invert function", () => {
4+
test("given empty object return empty object", () => {
5+
const currentOutput = invert({});
6+
const targetOutput = {};
7+
expect(currentOutput).toEqual(targetOutput);
8+
});
9+
});
10+
11+
test("given one key-value pair", () => {
12+
const currentOutput = invert({ age: 6 });
13+
const targetOutput = { 6: "age" };
14+
expect(currentOutput).toEqual(targetOutput);
15+
});
16+
17+
test("given non-string key-pair values", () => {
18+
const currentOutput = invert({
19+
age: 6,
20+
married: true,
21+
city: undefined,
22+
amount: null,
23+
});
24+
const targetOutput = {
25+
6: "age",
26+
true: "married",
27+
undefined: "city",
28+
null: "amount",
29+
};
30+
expect(currentOutput).toEqual(targetOutput);
31+
});
32+
33+
test("given non-string key-pair values with spaces or special characters", () => {
34+
const currentOutput = invert({
35+
age: "1 6",
36+
amount: "$67%",
37+
});
38+
const targetOutput = {
39+
"1 6": "age",
40+
"$67%": "amount",
41+
};
42+
expect(currentOutput).toEqual(targetOutput);
43+
});
44+
45+
test("given complex key-pair values", () => {
46+
const currentOutput = invert({
47+
age: [1, 5, 8, 6],
48+
amount: {
49+
berry: 5,
50+
vegs: 45,
51+
},
52+
});
53+
const targetOutput = {
54+
"1,5,8,6": "age",
55+
"[object Object]": "amount",
56+
};
57+
expect(currentOutput).toEqual(targetOutput);
58+
});

0 commit comments

Comments
 (0)