diff --git a/Sprint-2/interpret/invert.js b/Sprint-2/interpret/invert.js index bb353fb1f..79d324dca 100644 --- a/Sprint-2/interpret/invert.js +++ b/Sprint-2/interpret/invert.js @@ -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; diff --git a/Sprint-2/interpret/invert.test.js b/Sprint-2/interpret/invert.test.js new file mode 100644 index 000000000..7d2c49c7d --- /dev/null +++ b/Sprint-2/interpret/invert.test.js @@ -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" }); +});