From 1a5ecebda64505f4ecc3a8ef7074176959c046ee Mon Sep 17 00:00:00 2001 From: Shuheda Date: Sat, 14 Mar 2026 16:20:39 +0000 Subject: [PATCH 01/19] changed line 15 --- 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 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}`); From d68a6baec4994d3c965b4edd7f36f0c067cdaf55 Mon Sep 17 00:00:00 2001 From: Shuheda Date: Sat, 21 Mar 2026 11:31:55 +0000 Subject: [PATCH 02/19] completed address.js --- Sprint-2/debug/address.js | 1 + 1 file changed, 1 insertion(+) diff --git a/Sprint-2/debug/address.js b/Sprint-2/debug/address.js index 36d2f865d..0b8630bd4 100644 --- a/Sprint-2/debug/address.js +++ b/Sprint-2/debug/address.js @@ -1,4 +1,5 @@ // Predict and explain first... +// My house number is 42 // This code should log out the houseNumber from the address object // but it isn't working... From afca79f597449067999983d6b394a56917c0381b Mon Sep 17 00:00:00 2001 From: Shuheda Date: Sat, 21 Mar 2026 15:03:39 +0000 Subject: [PATCH 03/19] Made prediction in address.js --- Sprint-2/debug/address.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Sprint-2/debug/address.js b/Sprint-2/debug/address.js index 0b8630bd4..5fb429e7a 100644 --- a/Sprint-2/debug/address.js +++ b/Sprint-2/debug/address.js @@ -1,5 +1,8 @@ // Predict and explain first... -// My house number is 42 +// address is an object, not an array +// address[0] looks for a property named "0", +// which doesn't exist → returns undefined +// So the log will say: My house number is undefined // This code should log out the houseNumber from the address object // but it isn't working... From 8b250ab52808fe876ca450d427584fb24c19967b Mon Sep 17 00:00:00 2001 From: Shuheda Date: Sat, 21 Mar 2026 15:37:20 +0000 Subject: [PATCH 04/19] made predictions --- Sprint-2/debug/author.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/Sprint-2/debug/author.js b/Sprint-2/debug/author.js index 8c2125977..5a9e086e9 100644 --- a/Sprint-2/debug/author.js +++ b/Sprint-2/debug/author.js @@ -1,4 +1,8 @@ // Predict and explain first... +// The 'author' variable is an object, not an array or string. +// for..of loops require an iterable, but plain objects are not iterable. +// This will throw a TypeError: author is not iterable. +// Nothing will be logged to the console. // 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) { +for (const value of Object.values(author)) { console.log(value); -} +} \ No newline at end of file From 35c179a7db3b899d0db5ef936ab9137af659649f Mon Sep 17 00:00:00 2001 From: Shuheda Date: Sat, 21 Mar 2026 16:02:31 +0000 Subject: [PATCH 05/19] Prediction and console log updated --- Sprint-2/debug/recipe.js | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/Sprint-2/debug/recipe.js b/Sprint-2/debug/recipe.js index 6cbdd22cd..206b435fc 100644 --- a/Sprint-2/debug/recipe.js +++ b/Sprint-2/debug/recipe.js @@ -1,4 +1,8 @@ // Predict and explain first... +// The template literal ${recipe} attempts to convert the entire object to a string. +// Because objects aren't strings, it will log: "[object Object]". +// It also fails to access the specific 'ingredients' array or loop through them. +// The result will be one confusing line instead of a formatted list. // This program should log out the title, how many it serves and the ingredients. // Each ingredient should be logged on a new line @@ -10,6 +14,9 @@ const recipe = { ingredients: ["olive oil", "tomatoes", "salt", "pepper"], }; -console.log(`${recipe.title} serves ${recipe.serves} - ingredients: -${recipe}`); +console.log(`${recipe.title} serves ${recipe.serves}`); +console.log(" ingredients:"); + +for (const ing of recipe.ingredients) { + console.log(` - ${ing}`); +} From c5411f761311476506c944723121fef2d551af7e Mon Sep 17 00:00:00 2001 From: Shuheda Date: Sat, 21 Mar 2026 16:06:39 +0000 Subject: [PATCH 06/19] function changed --- Sprint-2/implement/contains.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/Sprint-2/implement/contains.js b/Sprint-2/implement/contains.js index cd779308a..4743d8171 100644 --- a/Sprint-2/implement/contains.js +++ b/Sprint-2/implement/contains.js @@ -1,3 +1,13 @@ -function contains() {} +function contains(object, propertyName) { + // return false if object is null/defined or not an object + if (object == null) return false; + // typeof check + reject arrays + if (typeof object !== "object") return false; + if (Array.isArray(object)) return false; + + // Use Object.hasOwn to check only own properties + + return Object.hasOwn(object, propertyName); +} module.exports = contains; From b1116344927b395341f1e84a62bac8ae1665681f Mon Sep 17 00:00:00 2001 From: Shuheda Date: Sat, 21 Mar 2026 16:31:56 +0000 Subject: [PATCH 07/19] test created using criteria --- Sprint-2/implement/contains.test.js | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/Sprint-2/implement/contains.test.js b/Sprint-2/implement/contains.test.js index 326bdb1f2..a4145a091 100644 --- a/Sprint-2/implement/contains.test.js +++ b/Sprint-2/implement/contains.test.js @@ -16,20 +16,40 @@ 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("returns true when the property exists in the object", () => { + expect(contains({ a: 1, b: 2 }, "a")).toBe(true); + expect(contains({ a: 1, b: 2 }, "b")).toBe(true); +}); // 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); + expect(contains({}, "toString")).toBe(false); +}); // Given an object with properties // When passed to contains with an existing property name // Then it should return true +test("returns true for existing property name", () => { + expect(contains({ name: "Alex" }, "name")).toBe(true); + expect(contains({ id: 42 }, "id")).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 for non-existent property name", () => { + expect(contains({ name: "Alex", age: 42 }, "email")).toBe(false); + expect(contains({ age: 54 }, "name")).toBe(false); +}); // Given invalid parameters like an array // When passed to contains // Then it should return false or throw an error +test("handles invalid parameters like an array", () => { + expect(contains([], "length")).toBe(false); + expect(contains(123, "length")).toBe(false); + expect(contains("string", "length")).toBe(false); +}); \ No newline at end of file From e3e970fcc7fe3f9a8924ef1a679eeb0a8d5fead0 Mon Sep 17 00:00:00 2001 From: Shuheda Date: Sat, 21 Mar 2026 16:41:22 +0000 Subject: [PATCH 08/19] function changed --- Sprint-2/implement/lookup.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/Sprint-2/implement/lookup.js b/Sprint-2/implement/lookup.js index a6746e07f..8c4747e31 100644 --- a/Sprint-2/implement/lookup.js +++ b/Sprint-2/implement/lookup.js @@ -1,5 +1,16 @@ -function createLookup() { +function createLookup(pairs) { // implementation here + // Creates empty object that will store our, + // country → currency + const lookup = {}; + // Loop through each pair in the input array + for (const [country, currency] of pairs) { + // Use the country code as they key + // Assigns the correspoding currency code as the value + lookup[country] = currency; + } + // returns the completed lookup object + return lookup; } module.exports = createLookup; From 6ae8335cc681c9d4cff2938e5566e7fae6e51ec3 Mon Sep 17 00:00:00 2001 From: Shuheda Date: Fri, 27 Mar 2026 19:30:43 +0000 Subject: [PATCH 09/19] Add notes --- Sprint-2/implement/lookup.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Sprint-2/implement/lookup.js b/Sprint-2/implement/lookup.js index 8c4747e31..4bbe88435 100644 --- a/Sprint-2/implement/lookup.js +++ b/Sprint-2/implement/lookup.js @@ -1,11 +1,11 @@ function createLookup(pairs) { - // implementation here - // Creates empty object that will store our, + // Implementation goes here + // Creates an empty object to store the lookup results // country → currency const lookup = {}; // Loop through each pair in the input array for (const [country, currency] of pairs) { - // Use the country code as they key + // Use the country code as the function key // Assigns the correspoding currency code as the value lookup[country] = currency; } From 0bb110a176c4ea4b38e03989e12cc009bc50f1df Mon Sep 17 00:00:00 2001 From: Shuheda Date: Fri, 27 Mar 2026 19:34:36 +0000 Subject: [PATCH 10/19] lookup.test.js completed --- Sprint-2/implement/lookup.test.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/Sprint-2/implement/lookup.test.js b/Sprint-2/implement/lookup.test.js index 547e06c5a..45539caae 100644 --- a/Sprint-2/implement/lookup.test.js +++ b/Sprint-2/implement/lookup.test.js @@ -1,7 +1,18 @@ 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 pairs = [ + ["US", "USD"], + ["CA", "CAD"], + ]; + const result = createLookup(pairs); + + expect(result).toEqual({ + US: "USD", + CA: "CAD", + }); +}); /* Create a lookup object of key value pairs from an array of code pairs From cf9390421f7d154d148702ce208131817e2a6f79 Mon Sep 17 00:00:00 2001 From: Shuheda Date: Fri, 27 Mar 2026 19:40:16 +0000 Subject: [PATCH 11/19] querystring.js completed --- Sprint-2/implement/querystring.js | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/Sprint-2/implement/querystring.js b/Sprint-2/implement/querystring.js index 45ec4e5f3..6db6da81d 100644 --- a/Sprint-2/implement/querystring.js +++ b/Sprint-2/implement/querystring.js @@ -1,6 +1,9 @@ function parseQueryString(queryString) { const queryParams = {}; if (queryString.length === 0) { + + // Handle empty query string + if (!queryString || queryString === "?") { return queryParams; } const keyValuePairs = queryString.split("&"); @@ -8,7 +11,25 @@ function parseQueryString(queryString) { for (const pair of keyValuePairs) { const [key, value] = pair.split("="); queryParams[key] = value; - } + // Remove the leading "?" if it exists + const cleaned = queryString.startsWith("?") + ? queryString.slice(1) + : queryString; + + // Split the query string into segments and filter out empty ones + const segments = cleaned.split("&").filter(Boolean); + + for (const segment of segments) { + // Split only on the first =, to allow for values that contain = + const eqIndex = segment.indexOf("="); + + const key = eqIndex === -1 ? segment : segment.slice(0, eqIndex); + const value = eqIndex === -1 ? "" : segment.slice(eqIndex + 1); + + if (key) { + // Decode percent-encoded characters in keys and values + queryParams[decodeURIComponent(key)] = decodeURIComponent(value); + } return queryParams; } From a9c1c10e804e2f695f2c35c88595021ed010fc93 Mon Sep 17 00:00:00 2001 From: Shuheda Date: Fri, 27 Mar 2026 19:48:27 +0000 Subject: [PATCH 12/19] corrected code --- Sprint-2/implement/querystring.js | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/Sprint-2/implement/querystring.js b/Sprint-2/implement/querystring.js index 6db6da81d..916d61a16 100644 --- a/Sprint-2/implement/querystring.js +++ b/Sprint-2/implement/querystring.js @@ -1,37 +1,32 @@ function parseQueryString(queryString) { const queryParams = {}; - if (queryString.length === 0) { // Handle empty query string if (!queryString || queryString === "?") { return queryParams; } - const keyValuePairs = queryString.split("&"); - for (const pair of keyValuePairs) { - const [key, value] = pair.split("="); - queryParams[key] = value; - // Remove the leading "?" if it exists + // Remove leading ? if present const cleaned = queryString.startsWith("?") ? queryString.slice(1) : queryString; - // Split the query string into segments and filter out empty ones + // Split into segments and ignore empty ones const segments = cleaned.split("&").filter(Boolean); for (const segment of segments) { - // Split only on the first =, to allow for values that contain = + // Split only on the first =, but everything after is a value const eqIndex = segment.indexOf("="); const key = eqIndex === -1 ? segment : segment.slice(0, eqIndex); const value = eqIndex === -1 ? "" : segment.slice(eqIndex + 1); if (key) { - // Decode percent-encoded characters in keys and values + // Decode percent-encoded characters in both key and value queryParams[decodeURIComponent(key)] = decodeURIComponent(value); } - - return queryParams; } +return queryParams; + module.exports = parseQueryString; From daff448bf2d73aa482d8ccd3d99e56b572097030 Mon Sep 17 00:00:00 2001 From: Shuheda Date: Fri, 27 Mar 2026 19:51:34 +0000 Subject: [PATCH 13/19] corrected code --- Sprint-2/implement/lookup.test.js | 1 - 1 file changed, 1 deletion(-) diff --git a/Sprint-2/implement/lookup.test.js b/Sprint-2/implement/lookup.test.js index 45539caae..ac1bdae6b 100644 --- a/Sprint-2/implement/lookup.test.js +++ b/Sprint-2/implement/lookup.test.js @@ -1,6 +1,5 @@ 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 pairs = [ ["US", "USD"], From e3f7768441b8089d7f3c68d388182944aa1aa469 Mon Sep 17 00:00:00 2001 From: Shuheda Date: Fri, 27 Mar 2026 19:54:49 +0000 Subject: [PATCH 14/19] fixed code querystring.js --- Sprint-2/implement/querystring.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sprint-2/implement/querystring.js b/Sprint-2/implement/querystring.js index 916d61a16..282fec921 100644 --- a/Sprint-2/implement/querystring.js +++ b/Sprint-2/implement/querystring.js @@ -28,5 +28,5 @@ function parseQueryString(queryString) { } return queryParams; - +} module.exports = parseQueryString; From 73fde8c5bdd96d8bce427315d977f7fd7c872815 Mon Sep 17 00:00:00 2001 From: Shuheda Date: Fri, 27 Mar 2026 19:57:16 +0000 Subject: [PATCH 15/19] completed code for querystring.test.js --- Sprint-2/implement/querystring.test.js | 52 +++++++++++++++++++++++++- 1 file changed, 50 insertions(+), 2 deletions(-) diff --git a/Sprint-2/implement/querystring.test.js b/Sprint-2/implement/querystring.test.js index 3e218b789..28ab418b6 100644 --- a/Sprint-2/implement/querystring.test.js +++ b/Sprint-2/implement/querystring.test.js @@ -3,10 +3,58 @@ // 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 multiple parameters", () => { + expect(parseQueryString("name=Alex&age=25&city=London")).toEqual({ + name: "Alex", + age: "25", + city: "London", + }); +}); + +test("handles empty value (key=)", () => { + expect(parseQueryString("search=&page=3")).toEqual({ + search: "", + page: "3", + }); +}); + +test("handles key without value", () => { + expect(parseQueryString("debug&sort=asc")).toEqual({ + debug: "", + sort: "asc", + }); +}); + +test("handles leading question mark", () => { + expect(parseQueryString("?q=javascript&lang=en")).toEqual({ + q: "javascript", + lang: "en", + }); +}); + +test("returns empty object when input is empty or just ?", () => { + expect(parseQueryString("")).toEqual({}); + expect(parseQueryString("?")).toEqual({}); +}); + +test("ignores trailing &", () => { + expect(parseQueryString("a=1&b=2&")).toEqual({ + a: "1", + b: "2", + }); +}); + +test("ignores repeated &", () => { + expect(parseQueryString("x=10&&y=20")).toEqual({ + x: "10", + y: "20", }); }); From 2e6d3b096609054722d40d1bbf599878a821cd3c Mon Sep 17 00:00:00 2001 From: Shuheda Date: Fri, 27 Mar 2026 20:01:03 +0000 Subject: [PATCH 16/19] tally.js completed --- Sprint-2/implement/tally.js | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/Sprint-2/implement/tally.js b/Sprint-2/implement/tally.js index f47321812..5aa5360d1 100644 --- a/Sprint-2/implement/tally.js +++ b/Sprint-2/implement/tally.js @@ -1,3 +1,23 @@ -function tally() {} +function tally(arr) { + // Check if the input is not an array + if (!Array.isArray(arr)) { + // If it's not an array, stop the function and return an error message + + throw new Error("Input must be an array"); + } + + // Create an empty object with no interited properties to store + // This prevents conflicts with built in properties like "toString" + const counts = Object.create(null); + + // Loop through each item in the array one by one + for (const item of arr) { + // If the item already exists in counts, add 1 to it + // If it doesn't exist yet (undefined), start it at 0 then add 1 + counts[item] = (counts[item] || 0) + 1; + } + // Return the finished counts object once all items have been tallied + return counts; +} module.exports = tally; From f7f99f3473a239e75e1d9393f8d30c7810a88520 Mon Sep 17 00:00:00 2001 From: Shuheda Date: Fri, 27 Mar 2026 20:05:18 +0000 Subject: [PATCH 17/19] completed tally.test.js --- Sprint-2/implement/tally.test.js | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/Sprint-2/implement/tally.test.js b/Sprint-2/implement/tally.test.js index 2ceffa8dd..595872839 100644 --- a/Sprint-2/implement/tally.test.js +++ b/Sprint-2/implement/tally.test.js @@ -19,16 +19,42 @@ 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 +test("tally returns an object containing the count for each unique item", () => { + expect(tally(["a", "a", "b", "c"])).toEqual({ + a: 2, + b: 1, + c: 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"); +test("tally on an empty array returns an empty object", () => { + expect(tally([])).toEqual({}); +}); + +// Given an array with a single item +// When passed to tally +// Then it should return a count of 1 for that item +test("tally on an array with one item returns count of 1", () => { + expect(tally(["a"])).toEqual({ + a: 1, + }); +}); // 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", "a"])).toEqual({ + a: 3, + }); +}); // 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("abc")).toThrow("Input must be an array"); +}); \ No newline at end of file From 27df5047a6006f2d433277ac38be2ed0c0d11b09 Mon Sep 17 00:00:00 2001 From: Shuheda Date: Fri, 27 Mar 2026 20:09:54 +0000 Subject: [PATCH 18/19] completed invert.js --- Sprint-2/interpret/invert.js | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/Sprint-2/interpret/invert.js b/Sprint-2/interpret/invert.js index bb353fb1f..7d4a1edfb 100644 --- a/Sprint-2/interpret/invert.js +++ b/Sprint-2/interpret/invert.js @@ -7,23 +7,37 @@ // E.g. invert({x : 10, y : 20}), target output: {"10": "x", "20": "y"} function invert(obj) { + // Check if the input is not an object + if (typeof obj !== "object" || obj === null || Array.isArray(obj)) { + throw new Error("Input must be an object"); + } 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 } +// 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 } +// The current return value when invert is called with { a: 1, b: 2 } is { key:2 } // c) What is the target return value when invert is called with {a : 1, b: 2} +// The target return value when invert is called with {a : 1, b: 2} is { "1": "a", "2": "b" } // c) What does Object.entries return? Why is it needed in this program? +// Object.entries converts an object into an array of [key, value] pairs, +// it's needed so you can loop through both the key and value at the same time // d) Explain why the current return value is different from the target output +// The bug is invertedObj.key = value - dot notation sets a literal key called "key", +// instead of using the variable. It also overwrites itseld on every loop iteration, +// so you only ever get the last value. // e) Fix the implementation of invert (and write tests to prove it's fixed!) +// Fixed implementation of invert and added tests cases to prove it's fixed. \ No newline at end of file From 8d8bee9cdb65d2678f3047a8aa86103d2679f5c7 Mon Sep 17 00:00:00 2001 From: Shuheda Date: Sat, 28 Mar 2026 10:14:39 +0000 Subject: [PATCH 19/19] invert.test.js created --- Sprint-2/interpret/invert.test.js | 38 +++++++++++++++++++++++++++++++ 1 file changed, 38 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..5a7e27fa2 --- /dev/null +++ b/Sprint-2/interpret/invert.test.js @@ -0,0 +1,38 @@ +const invert = require("./invert.js"); + +// Given an object +// When invert is passed this object +// Then it should swap the keys and values +test("invert swaps the keys and values of an object", () => { + expect(invert({ x: 10, y: 20 })).toEqual({ + 10: "x", + 20: "y", + }); +}); + +// Given an object with one key value pair +// When passed to invert +// Then it should return the swapped pair +test("invert swaps a single key value pair", () => { + expect(invert({ a: 1 })).toEqual({ + 1: "a", + }); +}); + +// Given an empty object +// When passed to invert +// Then it should return an empty object +test("invert on an empty object returns an empty object", () => { + expect(invert({})).toEqual({}); +}); + +// Given an invalid input like an array, string, number, null or undefined +// When passed to invert +// Then it should throw an error +test("invert throws an error for invalid input", () => { + expect(() => invert([])).toThrow("Input must be an object"); + expect(() => invert("hello")).toThrow("Input must be an object"); + expect(() => invert(123)).toThrow("Input must be an object"); + expect(() => invert(null)).toThrow("Input must be an object"); + expect(() => invert(undefined)).toThrow("Input must be an object"); +}); \ No newline at end of file