Skip to content

Commit ddbfe07

Browse files
committed
sprint 2 interpret task is done.
1 parent 9d50109 commit ddbfe07

2 files changed

Lines changed: 30 additions & 6 deletions

File tree

‎Sprint-2/interpret/invert.js‎

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,36 @@ function invert(obj) {
1010
const invertedObj = {};
1111

1212
for (const [key, value] of Object.entries(obj)) {
13-
invertedObj.key = value;
13+
//invertedObj.key = value;
14+
invertedObj[value] = key;
1415
}
1516

1617
return invertedObj;
1718
}
19+
/*
20+
console.log(invert({ a: 1 })); // {1:a}
21+
console.log(invert({ a: 1, b: 2 }));
22+
console.log(invert({ a: 1, b: 2 }));
23+
*/
1824

1925
// a) What is the current return value when invert is called with { a : 1 }
20-
26+
/*
27+
{
28+
key: 1;
29+
}
30+
*/
2131
// b) What is the current return value when invert is called with { a: 1, b: 2 }
22-
32+
/*
33+
{
34+
key: 2;
35+
}
36+
*/
2337
// c) What is the target return value when invert is called with {a : 1, b: 2}
24-
38+
// {"1":"a", "2":"b"}
2539
// c) What does Object.entries return? Why is it needed in this program?
26-
40+
//object.entries takes an objecet and returns an array of its key-value pairs.
2741
// d) Explain why the current return value is different from the target output
28-
42+
//because we use .key notation we are setting a property named "key" on our Object.
2943
// e) Fix the implementation of invert (and write tests to prove it's fixed!)
44+
45+
module.exports = invert;

‎Sprint-2/interpret/invert.test.js‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const invert = require("./invert.js");
2+
test("invert the key and values", () => {
3+
expect(invert({ a: 1 })).toEqual({ 1: "a" });
4+
});
5+
6+
test("invert the key and values", () => {
7+
expect(invert({ a: 1, b: 2 })).toEqual({ 1: "a", 2: b });
8+
});

0 commit comments

Comments
 (0)