Skip to content

Commit 93df342

Browse files
author
JorvanW
committed
added answers to invert.js and added a .test.js page to
1 parent f583fd7 commit 93df342

2 files changed

Lines changed: 15 additions & 6 deletions

File tree

‎Sprint-2/interpret/invert.js‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66

77
// E.g. invert({x : 10, y : 20}), target output: {"10": "x", "20": "y"}
88

9-
function invert(obj) {
10-
const invertedObj = {};
9+
function invert(obj) { // obj{} is the input of invert function
10+
const invertedObj = {}; // says that invertedObj is a empty object {}
1111

12-
for (const [key, value] of Object.entries(obj)) {
13-
invertedObj.key = value;
14-
}
12+
for (const [key, value] of Object.entries(obj)) { //'for' is looping the key and value of the object
13+
invertedObj[key] = value; // stored. Object.entires(obj) returns an array of array
14+
} // key in a bracket [] lets you use a variable called key
1515

1616
return invertedObj;
1717
}
@@ -31,6 +31,10 @@ function invert(obj) {
3131

3232
// d) Explain why the current return value is different from the target output
3333
// The current return value only shows {key: 2}. It doesn't show the first key and value only the second,
34-
// and it doesn't specify the second key. Its just defined as 'key'
34+
// and it doesn't specify the second key. Its just defined as 'key'.
3535

3636
// e) Fix the implementation of invert (and write tests to prove it's fixed!)
37+
38+
module.exports = invert;
39+
40+

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const invert = require("./invert.js");
2+
3+
test("When invert is passed, keys and values in the object should be swapped ",() => {
4+
expect({x : 10, y : 20}).toEqual({x : 10, y : 20});
5+
});

0 commit comments

Comments
 (0)