From 4f5f09b4c0f0c8493efc0ef370b357496a26598e Mon Sep 17 00:00:00 2001 From: Jak R-S <176810031+jakr-s@users.noreply.github.com> Date: Sat, 6 Dec 2025 10:59:38 +0000 Subject: [PATCH 01/11] complete /debug exercises --- Sprint-2/debug/address.js | 4 +++- Sprint-2/debug/author.js | 8 ++++++-- Sprint-2/debug/recipe.js | 4 +++- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/Sprint-2/debug/address.js b/Sprint-2/debug/address.js index 940a6af83..4f2fa6614 100644 --- a/Sprint-2/debug/address.js +++ b/Sprint-2/debug/address.js @@ -1,5 +1,7 @@ // Predict and explain first... +// Trying to access the address object using array notation `address[0]` instead of the property name `address.houseNumber`. + // This code should log out the houseNumber from the address object // but it isn't working... // Fix anything that isn't working @@ -12,4 +14,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..06be49433 100644 --- a/Sprint-2/debug/author.js +++ b/Sprint-2/debug/author.js @@ -1,5 +1,9 @@ // Predict and explain first... +// For..of loop only supports iterable objects (arrays), not objects +// Should use For..in instead +// Value needs to be accessed from the object using bracket notation + // 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 @@ -11,6 +15,6 @@ const author = { alive: true, }; -for (const value of author) { - console.log(value); +for (const value in author) { + console.log(author[value]); } diff --git a/Sprint-2/debug/recipe.js b/Sprint-2/debug/recipe.js index 6cbdd22cd..b70698350 100644 --- a/Sprint-2/debug/recipe.js +++ b/Sprint-2/debug/recipe.js @@ -1,5 +1,7 @@ // Predict and explain first... +// Logging the entire recipe object instead of iterating through each element in the ingredients array + // This program should log out the title, how many it serves and the ingredients. // Each ingredient should be logged on a new line // How can you fix it? @@ -12,4 +14,4 @@ const recipe = { console.log(`${recipe.title} serves ${recipe.serves} ingredients: -${recipe}`); +${recipe.ingredients.join("\n")}`); From e1c9439ea19152479dc2784b8b34022b60b55d9a Mon Sep 17 00:00:00 2001 From: Jak R-S <176810031+jakr-s@users.noreply.github.com> Date: Sat, 6 Dec 2025 11:38:31 +0000 Subject: [PATCH 02/11] implement contains function with tests --- Sprint-2/implement/contains.js | 17 ++++++++++++++++- Sprint-2/implement/contains.test.js | 18 +++++++++++++++++- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/Sprint-2/implement/contains.js b/Sprint-2/implement/contains.js index cd779308a..720321bb8 100644 --- a/Sprint-2/implement/contains.js +++ b/Sprint-2/implement/contains.js @@ -1,3 +1,18 @@ -function contains() {} +// Implement a function called contains that checks an object contains a +// particular property + +// E.g. contains({a: 1, b: 2}, 'a') // returns true +// as the object contains a key of 'a' + +// E.g. contains({a: 1, b: 2}, 'c') // returns false +// as the object doesn't contains a key of 'c' + +function contains(obj, property) { + if (obj === null || obj === undefined) { + return false; + } + + return property in obj; +} module.exports = contains; diff --git a/Sprint-2/implement/contains.test.js b/Sprint-2/implement/contains.test.js index 326bdb1f2..d5b94f8e1 100644 --- a/Sprint-2/implement/contains.test.js +++ b/Sprint-2/implement/contains.test.js @@ -16,20 +16,36 @@ 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 returns true for existing property", () => { + expect(contains({ a: 1, b: 2 }, "a")).toBe(true); + expect(contains({ a: 1, b: 2 }, "c")).toBe(false); +}); // 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")).toBe(false); +}); // Given an object with properties // When passed to contains with an existing property name // Then it should return true +test("contains returns true for multiple properties", () => { + expect(contains({ name: "John", age: 30, city: "NY" }, "age")).toBe(true); +}); // Given an object with properties // When passed to contains with a non-existent property name // Then it should return false +test("contains returns false for non-existent 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("contains returns false for non-object types", () => { + expect(contains(null, "a")).toBe(false); + expect(contains(undefined, "a")).toBe(false); +}); From e1377fcea1c70cc99fc06848debf4a997921deed Mon Sep 17 00:00:00 2001 From: Jak R-S <176810031+jakr-s@users.noreply.github.com> Date: Sat, 6 Dec 2025 11:38:53 +0000 Subject: [PATCH 03/11] refactor: remove unnecessary comments from contains function --- Sprint-2/implement/contains.js | 9 --------- 1 file changed, 9 deletions(-) diff --git a/Sprint-2/implement/contains.js b/Sprint-2/implement/contains.js index 720321bb8..f86322eb2 100644 --- a/Sprint-2/implement/contains.js +++ b/Sprint-2/implement/contains.js @@ -1,12 +1,3 @@ -// Implement a function called contains that checks an object contains a -// particular property - -// E.g. contains({a: 1, b: 2}, 'a') // returns true -// as the object contains a key of 'a' - -// E.g. contains({a: 1, b: 2}, 'c') // returns false -// as the object doesn't contains a key of 'c' - function contains(obj, property) { if (obj === null || obj === undefined) { return false; From 7bf8b1a04182ebedea501b81335973a15d9a74e9 Mon Sep 17 00:00:00 2001 From: Jak R-S <176810031+jakr-s@users.noreply.github.com> Date: Sat, 6 Dec 2025 23:16:36 +0000 Subject: [PATCH 04/11] implement createLookup function and tests --- Sprint-2/implement/lookup.js | 4 +-- Sprint-2/implement/lookup.test.js | 55 +++++++++++++------------------ 2 files changed, 24 insertions(+), 35 deletions(-) diff --git a/Sprint-2/implement/lookup.js b/Sprint-2/implement/lookup.js index a6746e07f..c62cd5935 100644 --- a/Sprint-2/implement/lookup.js +++ b/Sprint-2/implement/lookup.js @@ -1,5 +1,5 @@ -function createLookup() { - // implementation here +function createLookup(arr) { + return Object.fromEntries(arr); } module.exports = createLookup; diff --git a/Sprint-2/implement/lookup.test.js b/Sprint-2/implement/lookup.test.js index 547e06c5a..eb623f2a4 100644 --- a/Sprint-2/implement/lookup.test.js +++ b/Sprint-2/implement/lookup.test.js @@ -1,35 +1,24 @@ const createLookup = require("./lookup.js"); -test.todo("creates a country currency code lookup for multiple codes"); - -/* - -Create a lookup object of key value pairs from an array of code pairs - -Acceptance Criteria: - -Given - - An array of arrays representing country code and currency code pairs - e.g. [['US', 'USD'], ['CA', 'CAD']] - -When - - createLookup function is called with the country-currency array as an argument - -Then - - It should return an object where: - - The keys are the country codes - - The values are the corresponding currency codes - -Example -Given: [['US', 'USD'], ['CA', 'CAD']] - -When -createLookup(countryCurrencyPairs) is called - -Then -It should return: - { - 'US': 'USD', - 'CA': 'CAD' - } -*/ +describe("createLookup", () => { + test("creates a country currency code lookup for multiple codes", () => { + const input = [ + ["US", "USD"], + ["CA", "CAD"], + ]; + const result = createLookup(input); + expect(result).toEqual({ US: "USD", CA: "CAD" }); + }); + + test("creates a lookup with a single code pair", () => { + const input = [["GB", "GBP"]]; + const result = createLookup(input); + expect(result).toEqual({ GB: "GBP" }); + }); + + test("creates an empty lookup with an empty array", () => { + const input = []; + const result = createLookup(input); + expect(result).toEqual({}); + }); +}); From a9e23c2fac9de9ada1bd86914351baf26fbc9cbc Mon Sep 17 00:00:00 2001 From: Jak R-S <176810031+jakr-s@users.noreply.github.com> Date: Sat, 6 Dec 2025 23:19:14 +0000 Subject: [PATCH 05/11] refactor: remove unnecessary comments, put tests in describe block --- Sprint-2/implement/contains.test.js | 70 +++++++++-------------------- 1 file changed, 22 insertions(+), 48 deletions(-) diff --git a/Sprint-2/implement/contains.test.js b/Sprint-2/implement/contains.test.js index d5b94f8e1..f5ed844d9 100644 --- a/Sprint-2/implement/contains.test.js +++ b/Sprint-2/implement/contains.test.js @@ -1,51 +1,25 @@ const contains = require("./contains.js"); -/* -Implement a function called contains that checks an object contains a -particular property - -E.g. contains({a: 1, b: 2}, 'a') // returns true -as the object contains a key of 'a' - -E.g. contains({a: 1, b: 2}, 'c') // returns false -as the object doesn't contains a key of 'c' -*/ - -// Acceptance criteria: - -// 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 returns true for existing property", () => { - expect(contains({ a: 1, b: 2 }, "a")).toBe(true); - expect(contains({ a: 1, b: 2 }, "c")).toBe(false); -}); - -// Given an empty object -// When passed to contains -// Then it should return false -test("contains on empty object returns false", () => { - 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("contains returns true for multiple properties", () => { - expect(contains({ name: "John", age: 30, city: "NY" }, "age")).toBe(true); -}); - -// Given an object with properties -// When passed to contains with a non-existent property name -// Then it should return false -test("contains returns false for non-existent 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("contains returns false for non-object types", () => { - expect(contains(null, "a")).toBe(false); - expect(contains(undefined, "a")).toBe(false); +describe("contains", () => { + test("contains returns true for existing property", () => { + expect(contains({ a: 1, b: 2 }, "a")).toBe(true); + expect(contains({ a: 1, b: 2 }, "c")).toBe(false); + }); + + test("contains on empty object returns false", () => { + expect(contains({}, "a")).toBe(false); + }); + + test("contains returns true for multiple properties", () => { + expect(contains({ name: "John", age: 30, city: "NY" }, "age")).toBe(true); + }); + + test("contains returns false for non-existent property", () => { + expect(contains({ a: 1, b: 2 }, "c")).toBe(false); + }); + + test("contains returns false for non-object types", () => { + expect(contains(null, "a")).toBe(false); + expect(contains(undefined, "a")).toBe(false); + }); }); From 9559666cd2ff3e847236177510d97e3d2fa30eb5 Mon Sep 17 00:00:00 2001 From: Jak R-S <176810031+jakr-s@users.noreply.github.com> Date: Sun, 7 Dec 2025 16:32:53 +0000 Subject: [PATCH 06/11] implement tally function with tests --- Sprint-2/implement/tally.js | 17 +++++++++++- Sprint-2/implement/tally.test.js | 47 ++++++++++++-------------------- 2 files changed, 34 insertions(+), 30 deletions(-) diff --git a/Sprint-2/implement/tally.js b/Sprint-2/implement/tally.js index f47321812..4c2e5daca 100644 --- a/Sprint-2/implement/tally.js +++ b/Sprint-2/implement/tally.js @@ -1,3 +1,18 @@ -function tally() {} +function tally(arr) { + if (!Array.isArray(arr)) { + throw new Error(); + } + + let result = {}; + + for (const element of arr) { + if (element in result) { + result[element] += 1; + } else { + result[element] = 1; + } + } + return result; +} module.exports = tally; diff --git a/Sprint-2/implement/tally.test.js b/Sprint-2/implement/tally.test.js index 2ceffa8dd..c68b2cf54 100644 --- a/Sprint-2/implement/tally.test.js +++ b/Sprint-2/implement/tally.test.js @@ -1,34 +1,23 @@ const tally = require("./tally.js"); -/** - * tally array - * - * In this task, you'll need to implement a function called tally - * that will take a list of items and count the frequency of each item - * in an array - * - * For example: - * - * tally(['a']), target output: { a: 1 } - * tally(['a', 'a', 'a']), target output: { a: 3 } - * tally(['a', 'a', 'b', 'c']), target output: { a : 2, b: 1, c: 1 } - */ +describe("tally", () => { + test("tally on an empty array returns an empty object", () => { + expect(tally([])).toEqual({}); + }); -// Acceptance criteria: + test("tally with single item returns object with count of 1", () => { + expect(tally(["a"])).toEqual({ a: 1 }); + }); -// Given a function called tally -// When passed an array of items -// Then it should return an object containing the count for each unique item + test("tally with multiple items returns counts for each", () => { + expect(tally(["a", "a", "a"])).toEqual({ a: 3 }); + expect(tally(["a", "a", "b", "c"])).toEqual({ a: 2, b: 1, c: 1 }); + expect(tally(["x", "y", "x", "z", "y", "x"])).toEqual({ x: 3, y: 2, z: 1 }); + }); -// 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"); - -// Given an array with duplicate items -// When passed to tally -// Then it should return counts for each unique item - -// Given an invalid input like a string -// When passed to tally -// Then it should throw an error + test("tally with invalid input throws an error", () => { + expect(() => tally("invalid")).toThrow(); + expect(() => tally(null)).toThrow(); + expect(() => tally(undefined)).toThrow(); + }); +}); From d7107fd15b82ad4333015eea4a6cbde84ea933a3 Mon Sep 17 00:00:00 2001 From: Jak R-S <176810031+jakr-s@users.noreply.github.com> Date: Sun, 7 Dec 2025 16:46:32 +0000 Subject: [PATCH 07/11] fix: correct parseQueryString and implement additional tests --- Sprint-2/implement/querystring.js | 4 ++-- Sprint-2/implement/querystring.test.js | 16 ++++++++++++++-- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/Sprint-2/implement/querystring.js b/Sprint-2/implement/querystring.js index 45ec4e5f3..bbe349cd0 100644 --- a/Sprint-2/implement/querystring.js +++ b/Sprint-2/implement/querystring.js @@ -6,8 +6,8 @@ function parseQueryString(queryString) { 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/querystring.test.js b/Sprint-2/implement/querystring.test.js index 3e218b789..4ec30feb1 100644 --- a/Sprint-2/implement/querystring.test.js +++ b/Sprint-2/implement/querystring.test.js @@ -3,10 +3,22 @@ // Below is one test case for an edge case the implementation doesn't handle well. // Fix the implementation for this test, and try to think of as many other edge cases as possible - write tests and fix those too. -const parseQueryString = require("./querystring.js") +const parseQueryString = require("./querystring.js"); test("parses querystring values containing =", () => { expect(parseQueryString("equation=x=y+1")).toEqual({ - "equation": "x=y+1", + equation: "x=y+1", + }); +}); + +test("handles empty string", () => { + expect(parseQueryString("")).toEqual({}); +}); + +test("handles key without value", () => { + expect(parseQueryString("key1=value1&key2=&key3=value3")).toEqual({ + key1: "value1", + key2: "", + key3: "value3", }); }); From 5afff25bbe5e5b7e2c476a92079de37685cf9cf2 Mon Sep 17 00:00:00 2001 From: Jak R-S <176810031+jakr-s@users.noreply.github.com> Date: Sun, 7 Dec 2025 17:00:58 +0000 Subject: [PATCH 08/11] correct invert function implementation and add tests --- Sprint-2/interpret/invert.js | 10 +++++++++- Sprint-2/interpret/invert.test.js | 27 +++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) 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..6557bd307 100644 --- a/Sprint-2/interpret/invert.js +++ b/Sprint-2/interpret/invert.js @@ -10,20 +10,28 @@ function invert(obj) { const invertedObj = {}; for (const [key, value] of Object.entries(obj)) { - invertedObj.key = value; + invertedObj[value] = key; } return invertedObj; } // 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? +// Returns an array of key/values of the given object +// It is needed so that both the key and value can be destructured from the object and reassigned in the for..of loop // d) Explain why the current return value is different from the target output +// Dot notation is being used in the for..of loop, bracket notation should be used instead as it will become a computed property +// value and key should also be swapped inside the for..of loop // 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..262abf372 --- /dev/null +++ b/Sprint-2/interpret/invert.test.js @@ -0,0 +1,27 @@ +const invert = require("./invert.js"); + +describe("invert", () => { + test("inverts an empty object", () => { + const input = {}; + const result = invert(input); + expect(result).toEqual({}); + }); + + test("inverts a single key-value pair", () => { + const input = { a: 1 }; + const result = invert(input); + expect(result).toEqual({ 1: "a" }); + }); + + test("inverts multiple key-value pairs", () => { + const input = { a: 1, b: 2 }; + const result = invert(input); + expect(result).toEqual({ 1: "a", 2: "b" }); + }); + + test("inverts an object with string values", () => { + const input = { x: "hello", y: "world" }; + const result = invert(input); + expect(result).toEqual({ hello: "x", world: "y" }); + }); +}); From 21c0ee2ed8bc23c34982079f1d78241fd59aafa8 Mon Sep 17 00:00:00 2001 From: Jak R-S <176810031+jakr-s@users.noreply.github.com> Date: Wed, 10 Dec 2025 17:18:00 +0000 Subject: [PATCH 09/11] fix: update contains function to use Object.hasOwn --- Sprint-2/implement/contains.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sprint-2/implement/contains.js b/Sprint-2/implement/contains.js index f86322eb2..c61232cca 100644 --- a/Sprint-2/implement/contains.js +++ b/Sprint-2/implement/contains.js @@ -3,7 +3,7 @@ function contains(obj, property) { return false; } - return property in obj; + return Object.hasOwn(obj, property); } module.exports = contains; From 27c9c9d0229cd43b1f67822a5eb04a63935dd585 Mon Sep 17 00:00:00 2001 From: Jak R-S <176810031+jakr-s@users.noreply.github.com> Date: Wed, 10 Dec 2025 17:37:09 +0000 Subject: [PATCH 10/11] fix: decode keys and values in parseQueryString and add test case --- Sprint-2/implement/querystring.js | 4 +++- Sprint-2/implement/querystring.test.js | 7 +++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/Sprint-2/implement/querystring.js b/Sprint-2/implement/querystring.js index bbe349cd0..32b8c54af 100644 --- a/Sprint-2/implement/querystring.js +++ b/Sprint-2/implement/querystring.js @@ -7,7 +7,9 @@ function parseQueryString(queryString) { for (const pair of keyValuePairs) { const [key, ...valueParts] = pair.split("="); - queryParams[key] = valueParts.join("="); + const decodedKey = decodeURIComponent(key); + const decodedValue = decodeURIComponent(valueParts.join("=")); + queryParams[decodedKey] = decodedValue; } return queryParams; diff --git a/Sprint-2/implement/querystring.test.js b/Sprint-2/implement/querystring.test.js index 4ec30feb1..82628b2e0 100644 --- a/Sprint-2/implement/querystring.test.js +++ b/Sprint-2/implement/querystring.test.js @@ -22,3 +22,10 @@ test("handles key without value", () => { key3: "value3", }); }); + +test("should decode URL-encoded query strings", () => { + const queryString = "tags%5B%5D=hello%20world"; + const expected = { "tags[]": "hello world" }; + const result = parseQueryString(queryString); + expect(result).toEqual(expected); +}); From ae23446f50b042b33a76e3510f6d79f219bdd1e3 Mon Sep 17 00:00:00 2001 From: Jak R-S <176810031+jakr-s@users.noreply.github.com> Date: Wed, 10 Dec 2025 17:45:04 +0000 Subject: [PATCH 11/11] fix: update tally function to create empty object and add test for inherited properties --- Sprint-2/implement/tally.js | 2 +- Sprint-2/implement/tally.test.js | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/Sprint-2/implement/tally.js b/Sprint-2/implement/tally.js index 4c2e5daca..9578cb2fe 100644 --- a/Sprint-2/implement/tally.js +++ b/Sprint-2/implement/tally.js @@ -3,7 +3,7 @@ function tally(arr) { throw new Error(); } - let result = {}; + let result = Object.create(null); for (const element of arr) { if (element in result) { diff --git a/Sprint-2/implement/tally.test.js b/Sprint-2/implement/tally.test.js index c68b2cf54..499068667 100644 --- a/Sprint-2/implement/tally.test.js +++ b/Sprint-2/implement/tally.test.js @@ -20,4 +20,9 @@ describe("tally", () => { expect(() => tally(null)).toThrow(); expect(() => tally(undefined)).toThrow(); }); + + test("tally returns an object with no inherited properties", () => { + const result = tally(["a", "b", "a"]); + expect(Object.getPrototypeOf(result)).toBeNull(); + }); });