From c474e3968246b20337b040612acd4efc473f1114 Mon Sep 17 00:00:00 2001 From: Dagim-Daniel Date: Mon, 27 Jul 2026 17:01:45 +0100 Subject: [PATCH] invert task of the sprint 2 of section interpret has been done --- Sprint-2/interpret/invert.js | 25 +++++++++++++++++++------ Sprint-2/interpret/invert.test.js | 9 +++++++++ 2 files changed, 28 insertions(+), 6 deletions(-) create mode 100644 Sprint-2/interpret/invert.test.js 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" }); +});