Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions Sprint-2/interpret/invert.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,33 @@ function invert(obj) {
const invertedObj = {};

for (const [key, value] of Object.entries(obj)) {
invertedObj.key = value;
//invertedObj.key = value;
invertedObj[value] = key;
}

return invertedObj;
}
console.log(invert({ a: 1 }));
console.log(invert({ a: 1, b: 2 }));
console.log(invert({ a: 1, b: 2 }));

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

// a) What is the current return value when invert is called with { a : 1 }
/*{
key: 1;
}
*/
// b) What is the current return value when invert is called with { a: 1, b: 2 }

/*
{
key: 2;
}
*/
// c) What is the target return value when invert is called with {a : 1, b: 2}

// {"1":"a", "2":"b"}
// c) What does Object.entries return? Why is it needed in this program?

//object.entries takes an objecet and returns an array of its key-value pairs.
// d) Explain why the current return value is different from the target output

//because we use .key notation we are setting a property named "key" on our Object.
// e) Fix the implementation of invert (and write tests to prove it's fixed!)
9 changes: 9 additions & 0 deletions Sprint-2/interpret/invert.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const invert = require("./invert.js");

test("swap key with value", () => {
expect(invert({ a: 1 })).toEqual({ 1: "a" });
});

test("swap key with value", () => {
expect(invert({ a: 1, b: 2 })).toEqual({ 1: "a", 2: "b" });
});
Loading