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