Skip to content

Commit 33b2069

Browse files
committed
sprint 2 interpret section has been done
1 parent db8e129 commit 33b2069

2 files changed

Lines changed: 27 additions & 17 deletions

File tree

‎Sprint-2/interpret/invert.js‎

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,30 @@
1-
// Let's define how invert should work
2-
3-
// Given an object
4-
// When invert is passed this object
5-
// Then it should swap the keys and values in the object
6-
7-
// E.g. invert({x : 10, y : 20}), target output: {"10": "x", "20": "y"}
8-
91
function invert(obj) {
102
const invertedObj = {};
113

124
for (const [key, value] of Object.entries(obj)) {
13-
invertedObj.key = value;
5+
//invertedObj.key = value;
6+
invertedObj[value] = key;
147
}
158

169
return invertedObj;
1710
}
1811

1912
// a) What is the current return value when invert is called with { a : 1 }
20-
13+
/*
14+
{
15+
key: 1;
16+
}*/
2117
// b) What is the current return value when invert is called with { a: 1, b: 2 }
22-
18+
/*
19+
{
20+
key: 2;
21+
}
22+
*/
2323
// c) What is the target return value when invert is called with {a : 1, b: 2}
24-
25-
// c) What does Object.entries return? Why is it needed in this program?
26-
27-
// d) Explain why the current return value is different from the target output
28-
29-
// e) Fix the implementation of invert (and write tests to prove it's fixed!)
24+
// {"1":"a", "2":"b"}
25+
// d) What does Object.entries return? Why is it needed in this program?
26+
//object.entries takes an objecet and returns an array of its key-value pairs.
27+
// e) Explain why the current return value is different from the target output
28+
//because we use .key notation we are setting a property named "key" on our Object.
29+
// f) Fix the implementation of invert (and write tests to prove it's fixed!)
30+
module.exports = invert;

‎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)