From d1481372d3b39c36abc987b86579faeea2d73179 Mon Sep 17 00:00:00 2001 From: KayanatSuleman Date: Sun, 1 Mar 2026 21:28:01 +0000 Subject: [PATCH 01/31] Updating the file with my rediction --- Sprint-2/debug/address.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Sprint-2/debug/address.js b/Sprint-2/debug/address.js index 940a6af83..f83932257 100644 --- a/Sprint-2/debug/address.js +++ b/Sprint-2/debug/address.js @@ -1,4 +1,7 @@ // Predict and explain first... +//The issue with this program is that console.log is logging the address but is an obkect. address[0] only works for arrays. +//As address is an object, we must use a key rather than numeric indexes. +//Also there is no property called "0" inside address. // This code should log out the houseNumber from the address object // but it isn't working... From 437f3a9723b410c4be81fb73d3963ac037fd253d Mon Sep 17 00:00:00 2001 From: KayanatSuleman Date: Sun, 1 Mar 2026 21:29:03 +0000 Subject: [PATCH 02/31] Updating a spelling error --- Sprint-2/debug/address.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sprint-2/debug/address.js b/Sprint-2/debug/address.js index f83932257..c415e2ba6 100644 --- a/Sprint-2/debug/address.js +++ b/Sprint-2/debug/address.js @@ -1,5 +1,5 @@ // Predict and explain first... -//The issue with this program is that console.log is logging the address but is an obkect. address[0] only works for arrays. +//The issue with this program is that console.log is logging the address but is an object. address[0] only works for arrays. //As address is an object, we must use a key rather than numeric indexes. //Also there is no property called "0" inside address. From 2556a19d227b1adc1aaa27cd04c39a3d8e82a1d1 Mon Sep 17 00:00:00 2001 From: KayanatSuleman Date: Sun, 1 Mar 2026 21:29:59 +0000 Subject: [PATCH 03/31] Updating the program to correctly display the house number --- Sprint-2/debug/address.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sprint-2/debug/address.js b/Sprint-2/debug/address.js index c415e2ba6..d5d562082 100644 --- a/Sprint-2/debug/address.js +++ b/Sprint-2/debug/address.js @@ -15,4 +15,4 @@ const address = { postcode: "XYZ 123", }; -console.log(`My house number is ${address[0]}`); +console.log(`My house number is ${address.houseNumber}`); From 0503c00a486f4e8c4b5e61f380ab09a58e7cb739 Mon Sep 17 00:00:00 2001 From: KayanatSuleman Date: Sun, 1 Mar 2026 21:37:39 +0000 Subject: [PATCH 04/31] Predicting why the program for author.js will not work --- Sprint-2/debug/author.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Sprint-2/debug/author.js b/Sprint-2/debug/author.js index 8c2125977..1126f344c 100644 --- a/Sprint-2/debug/author.js +++ b/Sprint-2/debug/author.js @@ -1,4 +1,7 @@ // Predict and explain first... +// The issue with this program is that for.. of only works on iterable objects. +// Plain JavaScript objects are not iterable, so this will throw a "TypeError". +// To fix this, we can use Object.values(author) which converts the object's value into an array. // This program attempts to log out all the property values in the object. // But it isn't working. Explain why first and then fix the problem From c5af2fc4f2795a949097b7cb3fb1df932b9e4766 Mon Sep 17 00:00:00 2001 From: KayanatSuleman Date: Sun, 1 Mar 2026 21:39:50 +0000 Subject: [PATCH 05/31] Correcting the program to log all the object values of author --- Sprint-2/debug/author.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sprint-2/debug/author.js b/Sprint-2/debug/author.js index 1126f344c..1ee75deb0 100644 --- a/Sprint-2/debug/author.js +++ b/Sprint-2/debug/author.js @@ -14,6 +14,6 @@ const author = { alive: true, }; -for (const value of author) { +for (const value of Object.values(author)) { console.log(value); } From 0155ca2aabf7216dee73b99493d15e465077c2af Mon Sep 17 00:00:00 2001 From: KayanatSuleman Date: Sun, 1 Mar 2026 21:42:02 +0000 Subject: [PATCH 06/31] Ensuring that the correct valyes are logged using node --- Sprint-2/debug/author.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sprint-2/debug/author.js b/Sprint-2/debug/author.js index 1ee75deb0..3de90cec5 100644 --- a/Sprint-2/debug/author.js +++ b/Sprint-2/debug/author.js @@ -16,4 +16,4 @@ const author = { for (const value of Object.values(author)) { console.log(value); -} +} \ No newline at end of file From 1f294322f5f0b198717152904fd20851a2a5802e Mon Sep 17 00:00:00 2001 From: KayanatSuleman Date: Sun, 1 Mar 2026 21:51:12 +0000 Subject: [PATCH 07/31] Updating my prediction for recipe.js file --- Sprint-2/debug/recipe.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Sprint-2/debug/recipe.js b/Sprint-2/debug/recipe.js index 6cbdd22cd..a7fd5e705 100644 --- a/Sprint-2/debug/recipe.js +++ b/Sprint-2/debug/recipe.js @@ -1,4 +1,9 @@ // Predict and explain first... +//The issue with this program is that ${recipe} inserts the entire object into the template string. +// When an object is converted to a string, it becomes "[object Object]". +//Instead we would need to access the ingredients array. +//We can loop through recipe.ingredients or use join("\n") +//This will print each ingredients on a new line. // This program should log out the title, how many it serves and the ingredients. // Each ingredient should be logged on a new line From 09433a567ca9f372a4d31b209db9e61df24036e3 Mon Sep 17 00:00:00 2001 From: KayanatSuleman Date: Sun, 1 Mar 2026 21:58:44 +0000 Subject: [PATCH 08/31] Updating the program for reciple.js so that it runs as expected --- Sprint-2/debug/recipe.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Sprint-2/debug/recipe.js b/Sprint-2/debug/recipe.js index a7fd5e705..f39b7253b 100644 --- a/Sprint-2/debug/recipe.js +++ b/Sprint-2/debug/recipe.js @@ -16,5 +16,5 @@ const recipe = { }; console.log(`${recipe.title} serves ${recipe.serves} - ingredients: -${recipe}`); +ingredients: +${recipe.ingredients.join("\n")}`); \ No newline at end of file From 08b12a931d5ea5c212a1a094f7b55d9e0b9c8a61 Mon Sep 17 00:00:00 2001 From: KayanatSuleman Date: Sun, 1 Mar 2026 22:22:03 +0000 Subject: [PATCH 09/31] Adding the implementaion of the program for contains.js --- Sprint-2/implement/contains.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/Sprint-2/implement/contains.js b/Sprint-2/implement/contains.js index cd779308a..e6df0318b 100644 --- a/Sprint-2/implement/contains.js +++ b/Sprint-2/implement/contains.js @@ -1,3 +1,10 @@ -function contains() {} - +function contains(object, propertyName) { +if ( + typeof object !== "object" || + object === null || + Array.isArray(object)) { + return false; + } + return propertyName in object; +} module.exports = contains; From 557e87b5d2e22f23ca0d5559c88211b7b78db567 Mon Sep 17 00:00:00 2001 From: KayanatSuleman Date: Sun, 1 Mar 2026 22:29:21 +0000 Subject: [PATCH 10/31] Adding test for when object is empty --- Sprint-2/implement/contains.test.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Sprint-2/implement/contains.test.js b/Sprint-2/implement/contains.test.js index 326bdb1f2..5a3c31e6e 100644 --- a/Sprint-2/implement/contains.test.js +++ b/Sprint-2/implement/contains.test.js @@ -16,7 +16,9 @@ as the object doesn't contains a key of 'c' // Given a contains function // When passed an object and a property name // Then it should return true if the object contains the property, false otherwise - +test("contains on empty object return false", () => { + expect(contains({}, "a")).toBe(false); +}) // Given an empty object // When passed to contains // Then it should return false From c79969ba2bd7d21ee09b9334d80d4cd74b748722 Mon Sep 17 00:00:00 2001 From: KayanatSuleman Date: Sun, 1 Mar 2026 22:42:46 +0000 Subject: [PATCH 11/31] Adding test for when object contains property --- Sprint-2/implement/contains.test.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Sprint-2/implement/contains.test.js b/Sprint-2/implement/contains.test.js index 5a3c31e6e..f4c8595bc 100644 --- a/Sprint-2/implement/contains.test.js +++ b/Sprint-2/implement/contains.test.js @@ -22,7 +22,9 @@ test("contains on empty object return false", () => { // Given an empty object // When passed to contains // Then it should return false -test.todo("contains on empty object returns false"); +test("returns true when object contains property", () => { + expect(contains({a: 1,b: 2 }, "a")).toBe(true); +}); // Given an object with properties // When passed to contains with an existing property name From 3747d911bea249199f79944a3b2e89dd25da640f Mon Sep 17 00:00:00 2001 From: KayanatSuleman Date: Sun, 1 Mar 2026 22:52:16 +0000 Subject: [PATCH 12/31] Adding the other relevant tests and removing duplicated tests --- Sprint-2/implement/contains.test.js | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/Sprint-2/implement/contains.test.js b/Sprint-2/implement/contains.test.js index f4c8595bc..8ec0e6ded 100644 --- a/Sprint-2/implement/contains.test.js +++ b/Sprint-2/implement/contains.test.js @@ -16,24 +16,31 @@ as the object doesn't contains a key of 'c' // Given a contains function // When passed an object and a property name // Then it should return true if the object contains the property, false otherwise -test("contains on empty object return false", () => { - expect(contains({}, "a")).toBe(false); -}) + // Given an empty object // When passed to contains // Then it should return false -test("returns true when object contains property", () => { - expect(contains({a: 1,b: 2 }, "a")).toBe(true); +test("returns false for empty object", () => { + expect(contains({}, "a")).toBe(false); }); // Given an object with properties // When passed to contains with an existing property name // Then it should return true +test("returns true when object contains property", () => { + expect(contains({ a: 1, b: 2 }, "a")).toBe(true); +}); // Given an object with properties // When passed to contains with a non-existent property name // Then it should return false +test("returns false when object does not contain property", () => { + expect(contains({ a: 1, b: 2 }, "c")).toBe(false); +}); // Given invalid parameters like an array // When passed to contains // Then it should return false or throw an error +test("returns false for invalid input (array)", () => { + expect(contains(["a", "b"], "a")).toBe(false); +}); \ No newline at end of file From f440646e707cc74046c1956d9a4d5bb4cd91ba2c Mon Sep 17 00:00:00 2001 From: KayanatSuleman Date: Sun, 1 Mar 2026 23:25:54 +0000 Subject: [PATCH 13/31] Adding the program implementation for lookup.js --- Sprint-2/implement/lookup.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/Sprint-2/implement/lookup.js b/Sprint-2/implement/lookup.js index a6746e07f..b5414458a 100644 --- a/Sprint-2/implement/lookup.js +++ b/Sprint-2/implement/lookup.js @@ -1,5 +1,12 @@ -function createLookup() { - // implementation here +function createLookup(pairs) { +const result = {}; +for (const pair of pairs) { + const countryCode = pair[0]; + const currencyCode = pair[1]; + result[countryCode] = currencyCode; +} + +return result; } module.exports = createLookup; From 655fc119abe8937a3b1b3a175220cedaf0fb6524 Mon Sep 17 00:00:00 2001 From: KayanatSuleman Date: Sun, 1 Mar 2026 23:32:15 +0000 Subject: [PATCH 14/31] Adding relevant tests --- Sprint-2/implement/lookup.test.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Sprint-2/implement/lookup.test.js b/Sprint-2/implement/lookup.test.js index 547e06c5a..8a44c9655 100644 --- a/Sprint-2/implement/lookup.test.js +++ b/Sprint-2/implement/lookup.test.js @@ -1,6 +1,12 @@ const createLookup = require("./lookup.js"); -test.todo("creates a country currency code lookup for multiple codes"); +test("creates a country currency code lookup for multiple codes", () => { + const input = [['US', 'USD'], ['CA', 'CAD']]; + expect(createLookup(input)).toEqual({ + US: 'USD', + CA: 'CAD' + }); +}); /* From bd478a09b3fc95a815b7fa334189d16711fc5b93 Mon Sep 17 00:00:00 2001 From: KayanatSuleman Date: Mon, 2 Mar 2026 19:34:30 +0000 Subject: [PATCH 15/31] Correcting the program by testing the program in Node REPL --- Sprint-2/implement/querystring.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/Sprint-2/implement/querystring.js b/Sprint-2/implement/querystring.js index 45ec4e5f3..a9de58140 100644 --- a/Sprint-2/implement/querystring.js +++ b/Sprint-2/implement/querystring.js @@ -1,12 +1,22 @@ function parseQueryString(queryString) { const queryParams = {}; + if (queryString.length === 0) { return queryParams; } + const keyValuePairs = queryString.split("&"); for (const pair of keyValuePairs) { - const [key, value] = pair.split("="); + const equalsIndex = pair.indexOf("="); + + if (equalsIndex === -1) { + queryParams[pair] = ""; + continue; + } + const key = pair.slice(0, equalsIndex); + const value = pair.slice(equalsIndex + 1); + queryParams[key] = value; } From 695ad7584e777ecb72abeba2280acf2bd4573d17 Mon Sep 17 00:00:00 2001 From: KayanatSuleman Date: Mon, 2 Mar 2026 19:38:27 +0000 Subject: [PATCH 16/31] Adding a test to cover the empty input condition --- Sprint-2/implement/querystring.test.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Sprint-2/implement/querystring.test.js b/Sprint-2/implement/querystring.test.js index 3e218b789..3764296f8 100644 --- a/Sprint-2/implement/querystring.test.js +++ b/Sprint-2/implement/querystring.test.js @@ -10,3 +10,8 @@ test("parses querystring values containing =", () => { "equation": "x=y+1", }); }); + +// Empty Input +test("returns empty object for empty string", () => { + expect(parseQueryString("")).toEqual({}); +}); \ No newline at end of file From fb3189e173e344b1d8acde98887dca670753f73e Mon Sep 17 00:00:00 2001 From: KayanatSuleman Date: Mon, 2 Mar 2026 19:39:31 +0000 Subject: [PATCH 17/31] Adding a test case for single key/value pairs --- Sprint-2/implement/querystring.test.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Sprint-2/implement/querystring.test.js b/Sprint-2/implement/querystring.test.js index 3764296f8..cb69b6eb1 100644 --- a/Sprint-2/implement/querystring.test.js +++ b/Sprint-2/implement/querystring.test.js @@ -14,4 +14,9 @@ test("parses querystring values containing =", () => { // Empty Input test("returns empty object for empty string", () => { expect(parseQueryString("")).toEqual({}); +}); + +// Normal single pair +test("parses a single key/value pair", () => { + expect(parseQueryString("a=1")).toEqual({ a: "1" }); }); \ No newline at end of file From 3b6e4620cff19285162d10f6949a9e4d60e647ac Mon Sep 17 00:00:00 2001 From: KayanatSuleman Date: Mon, 2 Mar 2026 19:40:01 +0000 Subject: [PATCH 18/31] Adding a test case for normal multiple pairs --- Sprint-2/implement/querystring.test.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Sprint-2/implement/querystring.test.js b/Sprint-2/implement/querystring.test.js index cb69b6eb1..7258a737c 100644 --- a/Sprint-2/implement/querystring.test.js +++ b/Sprint-2/implement/querystring.test.js @@ -19,4 +19,9 @@ test("returns empty object for empty string", () => { // Normal single pair test("parses a single key/value pair", () => { expect(parseQueryString("a=1")).toEqual({ a: "1" }); +}); + +// Normal multiple pairs +test("parses multiple key/value pairs", () => { + expect(parseQueryString("a=1&b=2")).toEqual({ a: "1", b: "2" }); }); \ No newline at end of file From c4ab525411699b2487d4a1caae267ec9e35cc92c Mon Sep 17 00:00:00 2001 From: KayanatSuleman Date: Mon, 2 Mar 2026 19:41:59 +0000 Subject: [PATCH 19/31] Adding a test case for empty strings --- Sprint-2/implement/querystring.test.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Sprint-2/implement/querystring.test.js b/Sprint-2/implement/querystring.test.js index 7258a737c..c034de5fa 100644 --- a/Sprint-2/implement/querystring.test.js +++ b/Sprint-2/implement/querystring.test.js @@ -24,4 +24,9 @@ test("parses a single key/value pair", () => { // Normal multiple pairs test("parses multiple key/value pairs", () => { expect(parseQueryString("a=1&b=2")).toEqual({ a: "1", b: "2" }); +}); + +// Empty value should be an empty string +test("handles a key with an empty value", () => { + expect(parseQueryString("a=")).toEqual({ a: "" }); }); \ No newline at end of file From e161802c458334a1f5013336612ffe6025c6387f Mon Sep 17 00:00:00 2001 From: KayanatSuleman Date: Mon, 2 Mar 2026 20:05:23 +0000 Subject: [PATCH 20/31] Adding the implementation of the program for tally.js --- Sprint-2/implement/tally.js | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/Sprint-2/implement/tally.js b/Sprint-2/implement/tally.js index f47321812..7f9920847 100644 --- a/Sprint-2/implement/tally.js +++ b/Sprint-2/implement/tally.js @@ -1,3 +1,17 @@ -function tally() {} +function tally(items) { + if (!Array.isArray(items)){ + throw new Error("Input must be an array"); + } + const result = {}; + + for (const item of items) { + if (result[item]) { + result[item] += 1; + } else { + result[item] = 1; + } + } + return result; + } module.exports = tally; From d4d6ace90c524bb33f67daf9bd9b204915a552ca Mon Sep 17 00:00:00 2001 From: KayanatSuleman Date: Mon, 2 Mar 2026 20:09:09 +0000 Subject: [PATCH 21/31] Adding a test case for tallying on an empty array --- Sprint-2/implement/tally.test.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Sprint-2/implement/tally.test.js b/Sprint-2/implement/tally.test.js index 2ceffa8dd..ff3674536 100644 --- a/Sprint-2/implement/tally.test.js +++ b/Sprint-2/implement/tally.test.js @@ -23,7 +23,9 @@ const tally = require("./tally.js"); // Given an empty array // When passed to tally // Then it should return an empty object -test.todo("tally on an empty array returns an empty object"); +test("tally on an empty array returns an empty object", () => { + expect(tally([])).toEqual({}); +}); // Given an array with duplicate items // When passed to tally From b6f691ec16fc74589dde3193c1056f97d048d194 Mon Sep 17 00:00:00 2001 From: KayanatSuleman Date: Mon, 2 Mar 2026 20:10:27 +0000 Subject: [PATCH 22/31] Adding a test case for counting the freqency of items within an array --- Sprint-2/implement/tally.test.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Sprint-2/implement/tally.test.js b/Sprint-2/implement/tally.test.js index ff3674536..db1e36f21 100644 --- a/Sprint-2/implement/tally.test.js +++ b/Sprint-2/implement/tally.test.js @@ -30,6 +30,13 @@ test("tally on an empty array returns an empty object", () => { // Given an array with duplicate items // When passed to tally // Then it should return counts for each unique item +test("counts frequency of items in array", () => { + expect(tally(['a', 'a', 'b', 'c'])).toEqual({ + a: 2, + b: 1, + c: 1 + }); +}); // Given an invalid input like a string // When passed to tally From 34f680a9d2dc45de30f64454a14ec0f649add061 Mon Sep 17 00:00:00 2001 From: KayanatSuleman Date: Mon, 2 Mar 2026 20:11:38 +0000 Subject: [PATCH 23/31] Adding a test case for a invalid input --- Sprint-2/implement/tally.test.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Sprint-2/implement/tally.test.js b/Sprint-2/implement/tally.test.js index db1e36f21..811780f35 100644 --- a/Sprint-2/implement/tally.test.js +++ b/Sprint-2/implement/tally.test.js @@ -41,3 +41,6 @@ test("counts frequency of items in array", () => { // Given an invalid input like a string // When passed to tally // Then it should throw an error +test("throws error for invalid input", () => { + expect(() => tally("abc")).toThrow(); +}); \ No newline at end of file From efc7a77aa0ed4c879659bb41e60ff3ed0044eb2f Mon Sep 17 00:00:00 2001 From: KayanatSuleman Date: Mon, 2 Mar 2026 21:01:17 +0000 Subject: [PATCH 24/31] Answering questeion A) using node REPL --- Sprint-2/interpret/invert.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Sprint-2/interpret/invert.js b/Sprint-2/interpret/invert.js index bb353fb1f..916f07d76 100644 --- a/Sprint-2/interpret/invert.js +++ b/Sprint-2/interpret/invert.js @@ -16,8 +16,9 @@ function invert(obj) { return invertedObj; } +module.exports = invert; // a) What is the current return value when invert is called with { a : 1 } - +// The current return value when invert is called with { a : 1 } is { key: 1 } // b) What is the current return value when invert is called with { a: 1, b: 2 } // c) What is the target return value when invert is called with {a : 1, b: 2} From afc21f690c051101b3944f48821c077ee861b772 Mon Sep 17 00:00:00 2001 From: KayanatSuleman Date: Mon, 2 Mar 2026 21:49:48 +0000 Subject: [PATCH 25/31] Adding the answer to question B) using node REPL --- Sprint-2/interpret/invert.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Sprint-2/interpret/invert.js b/Sprint-2/interpret/invert.js index 916f07d76..ca56ca567 100644 --- a/Sprint-2/interpret/invert.js +++ b/Sprint-2/interpret/invert.js @@ -19,7 +19,10 @@ function invert(obj) { module.exports = invert; // a) What is the current return value when invert is called with { a : 1 } // The current return value when invert is called with { a : 1 } is { key: 1 } + // b) What is the current return value when invert is called with { a: 1, b: 2 } +// When calling invert({ a: 1, b: 2 }); the loop runs twice, the first iteration outputs invertedObj.key = 1l +// The second iterations overwrites the first, so the current value returned is { key: 2 } // c) What is the target return value when invert is called with {a : 1, b: 2} From e48cbff29b281d0bf363b592af5710b5d0e907ed Mon Sep 17 00:00:00 2001 From: KayanatSuleman Date: Mon, 2 Mar 2026 22:11:45 +0000 Subject: [PATCH 26/31] Adding the answer for question C) using node REPL to debug and fix the code --- Sprint-2/interpret/invert.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Sprint-2/interpret/invert.js b/Sprint-2/interpret/invert.js index ca56ca567..cb5cae46e 100644 --- a/Sprint-2/interpret/invert.js +++ b/Sprint-2/interpret/invert.js @@ -10,7 +10,7 @@ function invert(obj) { const invertedObj = {}; for (const [key, value] of Object.entries(obj)) { - invertedObj.key = value; + invertedObj[value] = key; } return invertedObj; @@ -25,6 +25,9 @@ module.exports = invert; // The second iterations overwrites the first, so the current value returned is { key: 2 } // c) What is the target return value when invert is called with {a : 1, b: 2} +// The target output should swap keys and value, therefore the output using Node REPL is { '1': 'a', '2': 'b' } +// After fixing the bug in the code. The values become keys, and the keys become the values. +// Object keys are stored as strings. // c) What does Object.entries return? Why is it needed in this program? From ee94815ad4cce72afa20c257def6eb630ee68b10 Mon Sep 17 00:00:00 2001 From: KayanatSuleman Date: Mon, 2 Mar 2026 22:51:57 +0000 Subject: [PATCH 27/31] Correct the question letters for easy readability --- Sprint-2/interpret/invert.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Sprint-2/interpret/invert.js b/Sprint-2/interpret/invert.js index cb5cae46e..bb5d6cb00 100644 --- a/Sprint-2/interpret/invert.js +++ b/Sprint-2/interpret/invert.js @@ -29,8 +29,8 @@ module.exports = invert; // After fixing the bug in the code. The values become keys, and the keys become the values. // Object keys are stored as strings. -// c) What does Object.entries return? Why is it needed in this program? +// d) What does Object.entries return? Why is it needed in this program? -// d) Explain why the current return value is different from the target output +// e) Explain why the current return value is different from the target output -// e) Fix the implementation of invert (and write tests to prove it's fixed!) +// f) Fix the implementation of invert (and write tests to prove it's fixed!) From bcc383cfadaf205bfaaa8c417459d84c696d8061 Mon Sep 17 00:00:00 2001 From: KayanatSuleman Date: Mon, 2 Mar 2026 22:59:44 +0000 Subject: [PATCH 28/31] Adding the answer for D) --- Sprint-2/interpret/invert.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Sprint-2/interpret/invert.js b/Sprint-2/interpret/invert.js index bb5d6cb00..79c94b8a3 100644 --- a/Sprint-2/interpret/invert.js +++ b/Sprint-2/interpret/invert.js @@ -30,6 +30,8 @@ module.exports = invert; // Object keys are stored as strings. // d) What does Object.entries return? Why is it needed in this program? +// Object.entries({ a: 1, b: 2}) return [["a", 1]], ["b", 2]] +// It is needed because objects are not iterable with for...of, but the entries array is, so we can loop key/value pairs. // e) Explain why the current return value is different from the target output From 899dadc327caffdc7b4d2ef9800d196c9d8ab40e Mon Sep 17 00:00:00 2001 From: KayanatSuleman Date: Mon, 2 Mar 2026 23:03:50 +0000 Subject: [PATCH 29/31] Adding the answer for question e) --- Sprint-2/interpret/invert.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Sprint-2/interpret/invert.js b/Sprint-2/interpret/invert.js index 79c94b8a3..18f688515 100644 --- a/Sprint-2/interpret/invert.js +++ b/Sprint-2/interpret/invert.js @@ -25,7 +25,7 @@ module.exports = invert; // The second iterations overwrites the first, so the current value returned is { key: 2 } // c) What is the target return value when invert is called with {a : 1, b: 2} -// The target output should swap keys and value, therefore the output using Node REPL is { '1': 'a', '2': 'b' } +// The target output should swap keys and value, therefore the output (using Node REPL) after the fix is { '1': 'a', '2': 'b' } // After fixing the bug in the code. The values become keys, and the keys become the values. // Object keys are stored as strings. @@ -34,5 +34,7 @@ module.exports = invert; // It is needed because objects are not iterable with for...of, but the entries array is, so we can loop key/value pairs. // e) Explain why the current return value is different from the target output +// In the code (before the bug fix) invertedObj.key = value creates a literal property called "key" each time, overwriting it. +// It does not use the variable key/value for dynamic property names, so it can't swap keys and values correctly. // f) Fix the implementation of invert (and write tests to prove it's fixed!) From 444d8d9065da60782d42a269a8a11264b83de50e Mon Sep 17 00:00:00 2001 From: KayanatSuleman Date: Mon, 2 Mar 2026 23:11:11 +0000 Subject: [PATCH 30/31] Adding a file to seperate tests and program logic for clear readibility and structure --- Sprint-2/interpret/invert.test.js | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 Sprint-2/interpret/invert.test.js diff --git a/Sprint-2/interpret/invert.test.js b/Sprint-2/interpret/invert.test.js new file mode 100644 index 000000000..dd11607e0 --- /dev/null +++ b/Sprint-2/interpret/invert.test.js @@ -0,0 +1,5 @@ +const invert = require("./invert.js"); + +test("inverts a single key value pair", () => { + expect(invert({ a: 1 })).toEqual({ "1": "a" }); +}); \ No newline at end of file From c885402388bbd83f3d29463955564a59a77c2f70 Mon Sep 17 00:00:00 2001 From: KayanatSuleman Date: Mon, 2 Mar 2026 23:13:34 +0000 Subject: [PATCH 31/31] Adding more test to cover other instances --- Sprint-2/interpret/invert.test.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/Sprint-2/interpret/invert.test.js b/Sprint-2/interpret/invert.test.js index dd11607e0..5e678a493 100644 --- a/Sprint-2/interpret/invert.test.js +++ b/Sprint-2/interpret/invert.test.js @@ -2,4 +2,15 @@ const invert = require("./invert.js"); test("inverts a single key value pair", () => { expect(invert({ a: 1 })).toEqual({ "1": "a" }); +}); + +test("inverts multiple key value pairs", () => { + expect(invert({ a: 1, b: 2 })).toEqual({ + "1": "a", + "2": "b" + }); +}); + +test("returns empty object when given empty object", () => { + expect(invert({})).toEqual({}); }); \ No newline at end of file