Skip to content
Closed
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
16 changes: 9 additions & 7 deletions Sprint-2/interpret/invert.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,22 @@ function invert(obj) {
const invertedObj = {};

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

return invertedObj;
}

// console.log(invert({ a: 1, b: 2 }), "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?

// The Object.entries() static method returns an array of a given object's own enumerable string-keyed property key-value pairs. It returns an array with the object's key-value pairs. e.g. [ [ 'a', 1 ], [ 'b', 2 ] ]
// d) Explain why the current return value is different from the target output

// This is because the function is not correctly inverting the key and value.
// e) Fix the implementation of invert (and write tests to prove it's fixed!)

module.exports = invert;
16 changes: 16 additions & 0 deletions Sprint-2/interpret/invert.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const invert = require("../interpret/invert");

test(`Should return {"10": "x", "20": "y"} when given { x: 10, y: 20 } `, () => {
expect(invert({ x: 10, y: 20 })).toEqual({ 10: "x", 20: "y" });
});

test(`Should return {"2": "bounce", "100": "high"} when given { bounce: 2, high: 100 } `, () => {
expect(invert({ bounce: 2, high: 100 })).toEqual({
2: "bounce",
100: "high",
});
});

test(`Should return {"foot": "ball"} when given { "ball": "foot" } `, () => {
expect(invert({ ball: "foot" })).toEqual({ foot: "ball" });
});
Loading