From 3fb57efb06f2219d8506693f29386ffe48cd2f91 Mon Sep 17 00:00:00 2001 From: Mouawia Elkhalifa Date: Mon, 23 Mar 2026 14:13:27 +0000 Subject: [PATCH 1/4] Complete Sprint 2 exercises --- Sprint-2/debug/address.js | 2 +- Sprint-2/debug/author.js | 2 +- Sprint-2/debug/recipe.js | 4 +-- Sprint-2/implement/contains.js | 8 ++++- Sprint-2/implement/contains.test.js | 15 ++++++-- Sprint-2/implement/lookup.js | 12 +++++-- Sprint-2/implement/lookup.test.js | 9 ++++- Sprint-2/implement/querystring.js | 9 +++-- Sprint-2/implement/tally.js | 27 +++++++++++++-- Sprint-2/implement/tally.test.js | 18 ++++++++-- Sprint-2/interpret/invert.js | 54 ++++++++++++++++++++++++----- 11 files changed, 136 insertions(+), 24 deletions(-) diff --git a/Sprint-2/debug/address.js b/Sprint-2/debug/address.js index 940a6af83..36d2f865d 100644 --- a/Sprint-2/debug/address.js +++ b/Sprint-2/debug/address.js @@ -12,4 +12,4 @@ const address = { postcode: "XYZ 123", }; -console.log(`My house number is ${address[0]}`); +console.log(`My house number is ${address.houseNumber}`); diff --git a/Sprint-2/debug/author.js b/Sprint-2/debug/author.js index 8c2125977..7ba8f4c75 100644 --- a/Sprint-2/debug/author.js +++ b/Sprint-2/debug/author.js @@ -11,6 +11,6 @@ const author = { alive: true, }; -for (const value of author) { +for (const value of Object.values(author)) { console.log(value); } diff --git a/Sprint-2/debug/recipe.js b/Sprint-2/debug/recipe.js index 6cbdd22cd..7b93b17b8 100644 --- a/Sprint-2/debug/recipe.js +++ b/Sprint-2/debug/recipe.js @@ -11,5 +11,5 @@ const recipe = { }; console.log(`${recipe.title} serves ${recipe.serves} - ingredients: -${recipe}`); +ingredients: +${recipe.ingredients.join("\n")}`); diff --git a/Sprint-2/implement/contains.js b/Sprint-2/implement/contains.js index cd779308a..81e8e7fab 100644 --- a/Sprint-2/implement/contains.js +++ b/Sprint-2/implement/contains.js @@ -1,3 +1,9 @@ -function contains() {} +function contains(obj, prop) { + if (typeof obj !== "object" || obj === null || Array.isArray(obj)) { + return false; + } + + return prop in obj; +} module.exports = contains; diff --git a/Sprint-2/implement/contains.test.js b/Sprint-2/implement/contains.test.js index 326bdb1f2..26f1d8d4c 100644 --- a/Sprint-2/implement/contains.test.js +++ b/Sprint-2/implement/contains.test.js @@ -20,16 +20,27 @@ as the object doesn't contains a key of 'c' // Given an empty object // When passed to contains // Then it should return false -test.todo("contains on empty object returns false"); +test("contains on empty object returns false", () =>{ + expect(contains({}, "a")).toEqual(false) +}); + // Given an object with properties // When passed to contains with an existing property name // Then it should return true +test("contains an object with propery exist, returns true", () =>{ + expect(contains({a:1, b:2}, "a")).toEqual(true) +}); // Given an object with properties // When passed to contains with a non-existent property name // Then it should return false - +test("return false when the property does not exist", () =>{ + expect(contains({a:1, b:3}, "c")).toEqual(false) +}); // Given invalid parameters like an array // When passed to contains // Then it should return false or throw an error +test("return false when paramters invaliud ", () =>{ + expect(contains([], "a")).toEqual(false) +}); diff --git a/Sprint-2/implement/lookup.js b/Sprint-2/implement/lookup.js index a6746e07f..f8253d2c4 100644 --- a/Sprint-2/implement/lookup.js +++ b/Sprint-2/implement/lookup.js @@ -1,5 +1,13 @@ -function createLookup() { - // implementation here +function createLookup(countryCurrencyPairs) { + const lookup = {}; + + for (const [countryCode, countryCurrency] of countryCurrencyPairs) { + lookup[countryCode] = countryCurrency; + } + + return lookup; } module.exports = createLookup; + + diff --git a/Sprint-2/implement/lookup.test.js b/Sprint-2/implement/lookup.test.js index 547e06c5a..b9538744e 100644 --- a/Sprint-2/implement/lookup.test.js +++ b/Sprint-2/implement/lookup.test.js @@ -1,6 +1,13 @@ 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",() =>{ + expect(createLookup([["US", "USD"], ["CA", "CAD"]])).toEqual( + {"US":"USD", + "CA": "CAD" + + }) + +}) /* diff --git a/Sprint-2/implement/querystring.js b/Sprint-2/implement/querystring.js index 45ec4e5f3..7453d4cb3 100644 --- a/Sprint-2/implement/querystring.js +++ b/Sprint-2/implement/querystring.js @@ -1,13 +1,18 @@ function parseQueryString(queryString) { + if (queryString.startsWith("?")) { + queryString = queryString.substring(1); + } + const queryParams = {}; if (queryString.length === 0) { return queryParams; } + const keyValuePairs = queryString.split("&"); for (const pair of keyValuePairs) { - const [key, value] = pair.split("="); - queryParams[key] = value; + const [key, ...valueParts] = pair.split("="); + queryParams[key] = valueParts.join("="); } return queryParams; diff --git a/Sprint-2/implement/tally.js b/Sprint-2/implement/tally.js index f47321812..1f3b66638 100644 --- a/Sprint-2/implement/tally.js +++ b/Sprint-2/implement/tally.js @@ -1,3 +1,26 @@ -function tally() {} +function tally(arr) { + // invalid input → throw error + if (!Array.isArray(arr)) { + throw new Error("Input must be an array"); + } -module.exports = tally; + // empty array → return empty object + if (arr.length === 0) { + return {}; + } + + const result = {}; + + // count items + for (const item of arr) { + if (result[item] === undefined) { + result[item] = 1; + } else { + result[item] = result[item] + 1; + } + } + + return result; +} + +module.exports = tally; \ No newline at end of file diff --git a/Sprint-2/implement/tally.test.js b/Sprint-2/implement/tally.test.js index 2ceffa8dd..8632463f3 100644 --- a/Sprint-2/implement/tally.test.js +++ b/Sprint-2/implement/tally.test.js @@ -18,17 +18,31 @@ const tally = require("./tally.js"); // Given a function called tally // When passed an array of items -// Then it should return an object containing the count for each unique item +// Then it should return an object containing the count for each unique ite // 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 // Then it should return counts for each unique item +test("tally counts duplicate items correctly", () => { + expect(tally(['a', 'a', 'b', 'c'])).toEqual({ + a: 2, + b: 1, + c: 1 + }); +}); + // Given an invalid input like a string // When passed to tally // Then it should throw an error +test("tally throws an error for invalid input", () => { + expect(() => tally("hello")).toThrow(); +}); \ No newline at end of file diff --git a/Sprint-2/interpret/invert.js b/Sprint-2/interpret/invert.js index bb353fb1f..8fd6f05f0 100644 --- a/Sprint-2/interpret/invert.js +++ b/Sprint-2/interpret/invert.js @@ -5,25 +5,63 @@ // Then it should swap the keys and values in the object // E.g. invert({x : 10, y : 20}), target output: {"10": "x", "20": "y"} - function invert(obj) { const invertedObj = {}; for (const [key, value] of Object.entries(obj)) { - invertedObj.key = value; + if (invertedObj[value] === undefined) { + invertedObj[value] = key; + } else if (Array.isArray(invertedObj[value])) { + invertedObj[value].push(key); + } else { + invertedObj[value] = [invertedObj[value], key]; + } } return invertedObj; } - // a) What is the current return value when invert is called with { a : 1 } - +//{"1": "a"}. // b) What is the current return value when invert is called with { a: 1, b: 2 } - +//{"1": "a", "2": "b"} // 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(obj) returns an array of [key, value] pairs, where each pair is stored as an array. +// It is needed in this program so we can loop through both the keys and values of the object at the same time when inverting it. +//it returns an array of arrays, where each inner array is a pair: [key, value]. +//The current return value is different from the target output because when multiple keys have +// the same value, they overwrite each other when inverted. This causes some values to be lost. +// e) Fix the implementation of invert (and write tests to prove it's fixed!) +function invert(obj) { + const invertedObj = {}; -// d) Explain why the current return value is different from the target output + for (const [key, value] of Object.entries(obj)) { + // Check if the value already exists as a key in our new object + if (invertedObj[value] === undefined) { + // First time seeing this: Save it as a string + invertedObj[value] = key; + } else if (Array.isArray(invertedObj[value])) { + // It's already an array: Add the new key to the list + invertedObj[value].push(key); + } else { + // It's a string: Turn it into an array and add the new key + invertedObj[value] = [invertedObj[value], key]; + } + } -// e) Fix the implementation of invert (and write tests to prove it's fixed!) + return invertedObj; +} + +// --- CONSOLE TESTS --- +console.log("Test 1 (Simple):", invert({ a: 1 })); +// Expected: { "1": "a" } + +console.log("Test 2 (No duplicates):", invert({ x: 10, y: 20 })); +// Expected: { "10": "x", "20": "y" } + +console.log("Test 3 (Collision):", invert({ a: 1, b: 1 })); +// Expected: { "1": ["a", "b"] } + +console.log("Test 4 (Triple collision):", invert({ a: 1, b: 1, c: 1 })); +// Expected: { "1": ["a", "b", "c"] } From 35a9a9b2286e2181e430c17ea46dc0baaa474d78 Mon Sep 17 00:00:00 2001 From: Mouawia Elkhalifa Date: Thu, 26 Mar 2026 22:22:52 +0000 Subject: [PATCH 2/4] fix: address mentor feedback for contains, tally, and invert --- Sprint-2/implement/contains.js | 2 +- Sprint-2/implement/contains.test.js | 6 ++-- Sprint-2/implement/querystring.js | 8 +++-- Sprint-2/implement/tally.js | 11 ++---- Sprint-2/implement/tally.test.js | 27 ++++++++++++--- Sprint-2/interpret/invert.js | 54 ++++------------------------- Sprint-2/interpret/invert.test.js | 33 ++++++++++++++++++ 7 files changed, 74 insertions(+), 67 deletions(-) create mode 100644 Sprint-2/interpret/invert.test.js diff --git a/Sprint-2/implement/contains.js b/Sprint-2/implement/contains.js index 81e8e7fab..3bc3fd5e2 100644 --- a/Sprint-2/implement/contains.js +++ b/Sprint-2/implement/contains.js @@ -3,7 +3,7 @@ function contains(obj, prop) { return false; } - return prop in obj; + return obj.hasOwn(obj, prop); } module.exports = contains; diff --git a/Sprint-2/implement/contains.test.js b/Sprint-2/implement/contains.test.js index 26f1d8d4c..c0fe3fb76 100644 --- a/Sprint-2/implement/contains.test.js +++ b/Sprint-2/implement/contains.test.js @@ -41,6 +41,6 @@ test("return false when the property does not exist", () =>{ // Given invalid parameters like an array // When passed to contains // Then it should return false or throw an error -test("return false when paramters invaliud ", () =>{ - expect(contains([], "a")).toEqual(false) -}); +test("returns false when input is an array", () => { + expect(contains([1, 2, 3], "0")).toEqual(false); +}); \ No newline at end of file diff --git a/Sprint-2/implement/querystring.js b/Sprint-2/implement/querystring.js index 7453d4cb3..cb1b233f1 100644 --- a/Sprint-2/implement/querystring.js +++ b/Sprint-2/implement/querystring.js @@ -11,11 +11,15 @@ function parseQueryString(queryString) { const keyValuePairs = queryString.split("&"); for (const pair of keyValuePairs) { + if (pair === "") continue; + const [key, ...valueParts] = pair.split("="); - queryParams[key] = valueParts.join("="); + const value = valueParts.join("="); + + queryParams[decodeURIComponent(key)] = decodeURIComponent(value); } return queryParams; } -module.exports = parseQueryString; +module.exports = parseQueryString; \ No newline at end of file diff --git a/Sprint-2/implement/tally.js b/Sprint-2/implement/tally.js index 1f3b66638..3f649133b 100644 --- a/Sprint-2/implement/tally.js +++ b/Sprint-2/implement/tally.js @@ -1,17 +1,10 @@ function tally(arr) { - // invalid input → throw error if (!Array.isArray(arr)) { throw new Error("Input must be an array"); } - // empty array → return empty object - if (arr.length === 0) { - return {}; - } - - const result = {}; + const result = Object.create(null); - // count items for (const item of arr) { if (result[item] === undefined) { result[item] = 1; @@ -23,4 +16,4 @@ function tally(arr) { return result; } -module.exports = tally; \ No newline at end of file +module.exports = tally; diff --git a/Sprint-2/implement/tally.test.js b/Sprint-2/implement/tally.test.js index 8632463f3..b39484b63 100644 --- a/Sprint-2/implement/tally.test.js +++ b/Sprint-2/implement/tally.test.js @@ -27,22 +27,39 @@ test("tally on an empty array returns an empty object", () => { expect(tally([])).toEqual({}); }); - // Given an array with duplicate items // When passed to tally // Then it should return counts for each unique item test("tally counts duplicate items correctly", () => { - expect(tally(['a', 'a', 'b', 'c'])).toEqual({ + expect(tally(["a", "a", "b", "c"])).toEqual({ a: 2, b: 1, - c: 1 + c: 1, }); }); - // Given an invalid input like a string // When passed to tally // Then it should throw an error test("tally throws an error for invalid input", () => { expect(() => tally("hello")).toThrow(); -}); \ No newline at end of file +}); +// Given an array containing keys that match built-in object properties +// When passed to tally +// Then it should correctly count them without conflicts from inherited properties +test("tally works correctly with reserved words like toString", () => { + const input = ["toString", "toString", "hasOwnProperty"]; + const expected = { + toString: 2, + hasOwnProperty: 1, + }; + expect(tally(input)).toEqual(expected); +}); +test("tally works correctly with reserved words like toString", () => { + const input = ["toString", "toString", "hasOwnProperty"]; + const expected = { + toString: 2, + hasOwnProperty: 1, + }; + expect(tally(input)).toEqual(expected); +}); diff --git a/Sprint-2/interpret/invert.js b/Sprint-2/interpret/invert.js index 8fd6f05f0..b7229d3d7 100644 --- a/Sprint-2/interpret/invert.js +++ b/Sprint-2/interpret/invert.js @@ -1,38 +1,8 @@ -// Let's define how invert should work - -// Given an object -// When invert is passed this object -// Then it should swap the keys and values in the object - -// E.g. invert({x : 10, y : 20}), target output: {"10": "x", "20": "y"} -function invert(obj) { - const invertedObj = {}; - - for (const [key, value] of Object.entries(obj)) { - if (invertedObj[value] === undefined) { - invertedObj[value] = key; - } else if (Array.isArray(invertedObj[value])) { - invertedObj[value].push(key); - } else { - invertedObj[value] = [invertedObj[value], key]; - } - } - - return invertedObj; -} -// a) What is the current return value when invert is called with { a : 1 } -//{"1": "a"}. -// b) What is the current return value when invert is called with { a: 1, b: 2 } -//{"1": "a", "2": "b"} -// 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(obj) returns an array of [key, value] pairs, where each pair is stored as an array. -// It is needed in this program so we can loop through both the keys and values of the object at the same time when inverting it. -//it returns an array of arrays, where each inner array is a pair: [key, value]. -//The current return value is different from the target output because when multiple keys have -// the same value, they overwrite each other when inverted. This causes some values to be lost. -// e) Fix the implementation of invert (and write tests to prove it's fixed!) +/** + * Invert Function + * Swaps keys and values of an object. If multiple keys share the same value, + * it groups those keys into an array to prevent overwriting data. + */ function invert(obj) { const invertedObj = {}; @@ -53,15 +23,5 @@ function invert(obj) { return invertedObj; } -// --- CONSOLE TESTS --- -console.log("Test 1 (Simple):", invert({ a: 1 })); -// Expected: { "1": "a" } - -console.log("Test 2 (No duplicates):", invert({ x: 10, y: 20 })); -// Expected: { "10": "x", "20": "y" } - -console.log("Test 3 (Collision):", invert({ a: 1, b: 1 })); -// Expected: { "1": ["a", "b"] } - -console.log("Test 4 (Triple collision):", invert({ a: 1, b: 1, c: 1 })); -// Expected: { "1": ["a", "b", "c"] } +// Ensure this is at the bottom so other files can use it +module.exports = invert; \ No newline at end of file diff --git a/Sprint-2/interpret/invert.test.js b/Sprint-2/interpret/invert.test.js new file mode 100644 index 000000000..2c7908744 --- /dev/null +++ b/Sprint-2/interpret/invert.test.js @@ -0,0 +1,33 @@ +const invert = require("./invert.js"); + +/** + * invert function tests + * These tests verify that keys and values are swapped correctly. + * They also confirm that 'collisions' (where two keys share a value) + * result in an array of keys to prevent data loss. + */ + +test("inverts a simple object", () => { + expect(invert({ a: 1 })).toEqual({ "1": "a" }); +}); + +test("inverts object with different values", () => { + expect(invert({ x: 10, y: 20 })).toEqual({ + "10": "x", + "20": "y", + }); +}); + +test("handles duplicate values (Collision)", () => { + // Verifies that a single value found twice becomes an array + expect(invert({ a: 1, b: 1 })).toEqual({ + "1": ["a", "b"], + }); +}); + +test("handles multiple duplicate values (Triple Collision)", () => { + // Verifies that three keys sharing a value are all captured + expect(invert({ a: 1, b: 1, c: 1 })).toEqual({ + "1": ["a", "b", "c"], + }); +}); \ No newline at end of file From 9c4818e566bc2a5195a7a554e43ff272689e8a56 Mon Sep 17 00:00:00 2001 From: Mouawia Elkhalifa Date: Fri, 27 Mar 2026 08:29:19 +0000 Subject: [PATCH 3/4] Fix invert logic/comments and strengthen contains tests for Sprint 2 --- Sprint-2/implement/contains.js | 5 +++-- Sprint-2/implement/contains.test.js | 21 ++++++++--------- Sprint-2/implement/querystring.js | 2 +- Sprint-2/implement/tally.test.js | 10 +++++---- Sprint-2/interpret/invert.js | 35 ++++++++++++++++++++--------- Sprint-2/interpret/invert.test.js | 33 --------------------------- 6 files changed, 46 insertions(+), 60 deletions(-) delete mode 100644 Sprint-2/interpret/invert.test.js diff --git a/Sprint-2/implement/contains.js b/Sprint-2/implement/contains.js index 3bc3fd5e2..40f559954 100644 --- a/Sprint-2/implement/contains.js +++ b/Sprint-2/implement/contains.js @@ -3,7 +3,8 @@ function contains(obj, prop) { return false; } - return obj.hasOwn(obj, prop); + // Use the static Object.hasOwn() method + return Object.hasOwn(obj, prop); } -module.exports = contains; +module.exports = contains; \ No newline at end of file diff --git a/Sprint-2/implement/contains.test.js b/Sprint-2/implement/contains.test.js index c0fe3fb76..7d55a7581 100644 --- a/Sprint-2/implement/contains.test.js +++ b/Sprint-2/implement/contains.test.js @@ -20,27 +20,28 @@ as the object doesn't contains a key of 'c' // Given an empty object // When passed to contains // Then it should return false -test("contains on empty object returns false", () =>{ - expect(contains({}, "a")).toEqual(false) +test("contains on empty object returns false", () => { + expect(contains({}, "a")).toEqual(false); }); - // Given an object with properties // When passed to contains with an existing property name // Then it should return true -test("contains an object with propery exist, returns true", () =>{ - expect(contains({a:1, b:2}, "a")).toEqual(true) +test("contains an object with propery exist, returns true", () => { + expect(contains({ a: 1, b: 2 }, "a")).toEqual(true); }); // Given an object with properties // When passed to contains with a non-existent property name // Then it should return false -test("return false when the property does not exist", () =>{ - expect(contains({a:1, b:3}, "c")).toEqual(false) +test("return false when the property does not exist", () => { + expect(contains({ a: 1, b: 3 }, "c")).toEqual(false); }); // Given invalid parameters like an array // When passed to contains // Then it should return false or throw an error -test("returns false when input is an array", () => { - expect(contains([1, 2, 3], "0")).toEqual(false); -}); \ No newline at end of file +test("returns false when input is an array, even if the index exists", () => { + // Index 0 exists in this array, but contains() should return false + // because the requirement is for plain objects only. + expect(contains(["apple", "banana"], 0)).toEqual(false); +}); diff --git a/Sprint-2/implement/querystring.js b/Sprint-2/implement/querystring.js index cb1b233f1..8a750be1f 100644 --- a/Sprint-2/implement/querystring.js +++ b/Sprint-2/implement/querystring.js @@ -22,4 +22,4 @@ function parseQueryString(queryString) { return queryParams; } -module.exports = parseQueryString; \ No newline at end of file +module.exports = parseQueryString; diff --git a/Sprint-2/implement/tally.test.js b/Sprint-2/implement/tally.test.js index b39484b63..0c19cd2b9 100644 --- a/Sprint-2/implement/tally.test.js +++ b/Sprint-2/implement/tally.test.js @@ -18,7 +18,7 @@ const tally = require("./tally.js"); // Given a function called tally // When passed an array of items -// Then it should return an object containing the count for each unique ite +// Then it should return an object containing the count for each unique item // Given an empty array // When passed to tally @@ -44,6 +44,7 @@ test("tally counts duplicate items correctly", () => { test("tally throws an error for invalid input", () => { expect(() => tally("hello")).toThrow(); }); + // Given an array containing keys that match built-in object properties // When passed to tally // Then it should correctly count them without conflicts from inherited properties @@ -55,11 +56,12 @@ test("tally works correctly with reserved words like toString", () => { }; expect(tally(input)).toEqual(expected); }); -test("tally works correctly with reserved words like toString", () => { - const input = ["toString", "toString", "hasOwnProperty"]; + +// Extra verification test to confirm pure object behavior +test("tally accurately counts prototype method names as strings", () => { + const input = ["toString", "toString"]; const expected = { toString: 2, - hasOwnProperty: 1, }; expect(tally(input)).toEqual(expected); }); diff --git a/Sprint-2/interpret/invert.js b/Sprint-2/interpret/invert.js index b7229d3d7..fdda515b2 100644 --- a/Sprint-2/interpret/invert.js +++ b/Sprint-2/interpret/invert.js @@ -1,21 +1,31 @@ -/** - * Invert Function - * Swaps keys and values of an object. If multiple keys share the same value, - * it groups those keys into an array to prevent overwriting data. - */ +// a) What is the current return value when invert is called with { a : 1 } +// {"1": "a"} + +// b) What is the current return value when invert is called with { a: 1, b: 2 } +// {"1": "a", "2": "b"} + +// 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? +// It returns an array of [key, value] pairs. It is needed to loop through both keys and values at the same time. + +// d) Explain why the current return value is different from the target output +// The original code used 'invertedObj.key = value', which literally created a property named "key". +// It also didn't handle collisions, so values with the same key would overwrite each other. + +// e) Fix the implementation of invert (and write tests to prove it's fixed!) function invert(obj) { const invertedObj = {}; for (const [key, value] of Object.entries(obj)) { - // Check if the value already exists as a key in our new object + // We use [value] for dynamic keys. + // We check if the key exists to handle collisions by creating an array. if (invertedObj[value] === undefined) { - // First time seeing this: Save it as a string invertedObj[value] = key; } else if (Array.isArray(invertedObj[value])) { - // It's already an array: Add the new key to the list invertedObj[value].push(key); } else { - // It's a string: Turn it into an array and add the new key invertedObj[value] = [invertedObj[value], key]; } } @@ -23,5 +33,10 @@ function invert(obj) { return invertedObj; } -// Ensure this is at the bottom so other files can use it +// --- CONSOLE TESTS TO PROVE IT IS FIXED --- +console.log("Test 1 (Simple):", invert({ a: 1 })); +console.log("Test 2 (No duplicates):", invert({ x: 10, y: 20 })); +console.log("Test 3 (Collision):", invert({ a: 1, b: 1 })); +console.log("Test 4 (Triple collision):", invert({ a: 1, b: 1, c: 1 })); + module.exports = invert; \ No newline at end of file diff --git a/Sprint-2/interpret/invert.test.js b/Sprint-2/interpret/invert.test.js deleted file mode 100644 index 2c7908744..000000000 --- a/Sprint-2/interpret/invert.test.js +++ /dev/null @@ -1,33 +0,0 @@ -const invert = require("./invert.js"); - -/** - * invert function tests - * These tests verify that keys and values are swapped correctly. - * They also confirm that 'collisions' (where two keys share a value) - * result in an array of keys to prevent data loss. - */ - -test("inverts a simple object", () => { - expect(invert({ a: 1 })).toEqual({ "1": "a" }); -}); - -test("inverts object with different values", () => { - expect(invert({ x: 10, y: 20 })).toEqual({ - "10": "x", - "20": "y", - }); -}); - -test("handles duplicate values (Collision)", () => { - // Verifies that a single value found twice becomes an array - expect(invert({ a: 1, b: 1 })).toEqual({ - "1": ["a", "b"], - }); -}); - -test("handles multiple duplicate values (Triple Collision)", () => { - // Verifies that three keys sharing a value are all captured - expect(invert({ a: 1, b: 1, c: 1 })).toEqual({ - "1": ["a", "b", "c"], - }); -}); \ No newline at end of file From 42a933ec5f4ac0e9b5e716d21f4a308cb7e2d91c Mon Sep 17 00:00:00 2001 From: Mouawia Elkhalifa Date: Fri, 27 Mar 2026 12:39:40 +0000 Subject: [PATCH 4/4] Final fix: Corrected invert answers (a-b) and strengthened contains tests --- Sprint-2/interpret/invert.js | 38 ++++++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/Sprint-2/interpret/invert.js b/Sprint-2/interpret/invert.js index fdda515b2..88da66dd9 100644 --- a/Sprint-2/interpret/invert.js +++ b/Sprint-2/interpret/invert.js @@ -1,26 +1,27 @@ // a) What is the current return value when invert is called with { a : 1 } -// {"1": "a"} +// { key: 1 } // b) What is the current return value when invert is called with { a: 1, b: 2 } -// {"1": "a", "2": "b"} +// { key: 2 } -// c) What is the target return value when invert is called with {a : 1, b: 2} -// {"1": "a", "2": "b"} +// 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? -// It returns an array of [key, value] pairs. It is needed to loop through both keys and values at the same time. +// d) What does Object.entries return? Why is it needed in this program? +// It returns an array of [key, value] pairs. +// It is needed so we can loop through both keys and values at the same time when inverting the object. -// d) Explain why the current return value is different from the target output -// The original code used 'invertedObj.key = value', which literally created a property named "key". -// It also didn't handle collisions, so values with the same key would overwrite each other. +// e) Explain why the current return value is different from the target output +// The original code used 'invertedObj.key = value', which creates a property literally named "key" +// instead of using the value dynamically. +// This causes each iteration to overwrite the previous one. +// It also does not handle collisions (multiple keys with the same value). -// 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!) function invert(obj) { const invertedObj = {}; for (const [key, value] of Object.entries(obj)) { - // We use [value] for dynamic keys. - // We check if the key exists to handle collisions by creating an array. if (invertedObj[value] === undefined) { invertedObj[value] = key; } else if (Array.isArray(invertedObj[value])) { @@ -34,9 +35,16 @@ function invert(obj) { } // --- CONSOLE TESTS TO PROVE IT IS FIXED --- -console.log("Test 1 (Simple):", invert({ a: 1 })); -console.log("Test 2 (No duplicates):", invert({ x: 10, y: 20 })); -console.log("Test 3 (Collision):", invert({ a: 1, b: 1 })); +console.log("Test 1 (Simple):", invert({ a: 1 })); +// Expected: { '1': 'a' } + +console.log("Test 2 (No duplicates):", invert({ x: 10, y: 20 })); +// Expected: { '10': 'x', '20': 'y' } + +console.log("Test 3 (Collision):", invert({ a: 1, b: 1 })); +// Expected: { '1': ['a', 'b'] } + console.log("Test 4 (Triple collision):", invert({ a: 1, b: 1, c: 1 })); +// Expected: { '1': ['a', 'b', 'c'] } module.exports = invert; \ No newline at end of file