Skip to content

Commit c474e39

Browse files
committed
invert task of the sprint 2 of section interpret has been done
1 parent de6889c commit c474e39

2 files changed

Lines changed: 28 additions & 6 deletions

File tree

‎Sprint-2/interpret/invert.js‎

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,33 @@ 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+
console.log(invert({ a: 1 }));
20+
console.log(invert({ a: 1, b: 2 }));
21+
console.log(invert({ a: 1, b: 2 }));
1822

19-
// a) What is the current return value when invert is called with { a : 1 }
23+
module.exports = invert;
2024

25+
// a) What is the current return value when invert is called with { a : 1 }
26+
/*{
27+
key: 1;
28+
}
29+
*/
2130
// b) What is the current return value when invert is called with { a: 1, b: 2 }
22-
31+
/*
32+
{
33+
key: 2;
34+
}
35+
*/
2336
// c) What is the target return value when invert is called with {a : 1, b: 2}
24-
37+
// {"1":"a", "2":"b"}
2538
// c) What does Object.entries return? Why is it needed in this program?
26-
39+
//object.entries takes an objecet and returns an array of its key-value pairs.
2740
// d) Explain why the current return value is different from the target output
28-
41+
//because we use .key notation we are setting a property named "key" on our Object.
2942
// e) Fix the implementation of invert (and write tests to prove it's fixed!)

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

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

0 commit comments

Comments
 (0)