From 5a797a0d09b62eeb82bb1fcc28aa8078650cd222 Mon Sep 17 00:00:00 2001 From: cywong Date: Wed, 29 Jul 2026 15:20:05 +0100 Subject: [PATCH 01/16] Fix iteration method for author object Replaced for-of loop with for-in loop to iterate over object properties. --- Sprint-2/debug/author.js | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/Sprint-2/debug/author.js b/Sprint-2/debug/author.js index 8c2125977..50447e4ff 100644 --- a/Sprint-2/debug/author.js +++ b/Sprint-2/debug/author.js @@ -11,6 +11,14 @@ const author = { alive: true, }; -for (const value of author) { - console.log(value); +// for (const value of author) { +// console.log(value); +// } + +for (const key in author) { + console.log(author[key]); } + +// the iteration method does not work for the object. +// TypeError TypeError: author is not iterable will be shown +// Change the correct method with the object From 013e56da657df0d240c163b6bc1dbd7a7ffb40b0 Mon Sep 17 00:00:00 2001 From: cywong Date: Wed, 29 Jul 2026 15:22:15 +0100 Subject: [PATCH 02/16] Update address.js --- Sprint-2/debug/address.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Sprint-2/debug/address.js b/Sprint-2/debug/address.js index 940a6af83..d621040b2 100644 --- a/Sprint-2/debug/address.js +++ b/Sprint-2/debug/address.js @@ -12,4 +12,9 @@ const address = { postcode: "XYZ 123", }; -console.log(`My house number is ${address[0]}`); +console.log(`My house number is ${address.houseNumber}`); +// console.log(`My house number is ${address[0]}`); + +// The original syntax works for array and not for object. +// it will show undefine message +// Instead use the correct . operator to access the object field values From 5e4611f6f1874cbfb4770cc688d0279a0059985d Mon Sep 17 00:00:00 2001 From: cywong Date: Wed, 29 Jul 2026 15:28:42 +0100 Subject: [PATCH 03/16] Display recipe ingredients in the console Uncommented the loop to display ingredients and removed old console log. --- Sprint-2/debug/recipe.js | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/Sprint-2/debug/recipe.js b/Sprint-2/debug/recipe.js index 6cbdd22cd..e279a0145 100644 --- a/Sprint-2/debug/recipe.js +++ b/Sprint-2/debug/recipe.js @@ -10,6 +10,14 @@ const recipe = { ingredients: ["olive oil", "tomatoes", "salt", "pepper"], }; -console.log(`${recipe.title} serves ${recipe.serves} - ingredients: -${recipe}`); +//console.log(`${recipe.title} serves ${recipe.serves} +// ingredients: +//${recipe}`); + +for (const ingredient of recipe.ingredients) { + console.log(ingredient); +} + + +// [object Object] will be shown because JavaScript converts an object to a string, it becomes "[object object]. +// Use the syntax to access the ingredients directly From e720a7c93d2b3bce98cf78435b7e5ffd647544b7 Mon Sep 17 00:00:00 2001 From: cywong Date: Sun, 2 Aug 2026 18:55:44 +0100 Subject: [PATCH 04/16] Enhance contains function with input validation Add validation to the contains function to check if the input is a valid object and if the property exists. --- 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..4f8354667 100644 --- a/Sprint-2/implement/contains.js +++ b/Sprint-2/implement/contains.js @@ -1,3 +1,13 @@ -function contains() {} +function contains() { + + // Return false if obj is null, undefined, an array, or not a non-null object + if (typeof obj !== "object" || obj === null || Array.isArray(obj)) { + return false; + } + + // Check if the property exists directly on the object + return Object.prototype.hasOwnProperty.call(obj, prop); + +} module.exports = contains; From b7442ae24be91f0048cf46c0dd1802c0b08b0d52 Mon Sep 17 00:00:00 2001 From: cywong Date: Sun, 2 Aug 2026 18:59:02 +0100 Subject: [PATCH 05/16] Implement tests for contains function --- Sprint-2/implement/contains.test.js | 44 ++++++++++++++++++----------- 1 file changed, 28 insertions(+), 16 deletions(-) diff --git a/Sprint-2/implement/contains.test.js b/Sprint-2/implement/contains.test.js index 326bdb1f2..49b9531ba 100644 --- a/Sprint-2/implement/contains.test.js +++ b/Sprint-2/implement/contains.test.js @@ -17,19 +17,31 @@ as the object doesn't contains a key of 'c' // When passed an object and a property name // Then it should return true if the object contains the property, false otherwise -// Given an empty object -// When passed to contains -// Then it should return false -test.todo("contains on empty object returns false"); - -// Given an object with properties -// When passed to contains with an existing property name -// Then it should return true - -// Given an object with properties -// When passed to contains with a non-existent property name -// Then it should return false - -// Given invalid parameters like an array -// When passed to contains -// Then it should return false or throw an error + // 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("returns true when passed an existing property name", () => { + const inputObj = { a: 1, b: 2 }; + }); + + // Given an object with properties + // When passed to contains with a non-existent property name + // Then it should return false + test("returns false when passed a non-existent property name", () => { + const inputObj = { a: 1, b: 2 }; + expect(contains(inputObj, "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 when passed invalid parameters like arrays or primitives", () => { + expect(contains([1, 2, 3], "0")).toBe(false); + }); From d2af99120d80b29dad1f87d9297347df054dd610 Mon Sep 17 00:00:00 2001 From: cywong Date: Sun, 2 Aug 2026 19:03:13 +0100 Subject: [PATCH 06/16] Add createLookup function in lookup.js Implement createLookup function to return country-currency pairs. --- Sprint-2/implement/lookup.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Sprint-2/implement/lookup.js b/Sprint-2/implement/lookup.js index a6746e07f..63ed72807 100644 --- a/Sprint-2/implement/lookup.js +++ b/Sprint-2/implement/lookup.js @@ -1,4 +1,8 @@ function createLookup() { + + return Object.fromEntries(countryCurrencyPairs); + + // implementation here } From 3af6a3bfb51b38763811ed1406dbf078226e6006 Mon Sep 17 00:00:00 2001 From: cywong Date: Sun, 2 Aug 2026 19:04:13 +0100 Subject: [PATCH 07/16] Implement test for country currency code lookup --- Sprint-2/implement/lookup.test.js | 51 +++++++++++-------------------- 1 file changed, 18 insertions(+), 33 deletions(-) diff --git a/Sprint-2/implement/lookup.test.js b/Sprint-2/implement/lookup.test.js index 547e06c5a..e2f8f1801 100644 --- a/Sprint-2/implement/lookup.test.js +++ b/Sprint-2/implement/lookup.test.js @@ -1,35 +1,20 @@ 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' - } -*/ +test("creates a country currency code lookup for multiple codes", () => { + // Given + const input = [ + ["US", "USD"], + ["CA", "CAD"], + ]; + + const expectedOutput = { + US: "USD", + CA: "CAD", + }; + + // When + const result = createLookup(input); + + // Then + expect(result).toEqual(expectedOutput); +}); From c9a22bbabcc42d8ef19c334371343ad7de71de78 Mon Sep 17 00:00:00 2001 From: cywong Date: Mon, 3 Aug 2026 13:47:00 +0100 Subject: [PATCH 08/16] Refactor parseQueryString for better handling Enhanced the parseQueryString function to handle empty query strings and duplicate keys. Improved decoding of parameters and ensured proper handling of key-value pairs. --- Sprint-2/implement/querystring.js | 43 ++++++++++++++++++++++++++++--- 1 file changed, 40 insertions(+), 3 deletions(-) diff --git a/Sprint-2/implement/querystring.js b/Sprint-2/implement/querystring.js index 45ec4e5f3..7bf291348 100644 --- a/Sprint-2/implement/querystring.js +++ b/Sprint-2/implement/querystring.js @@ -1,13 +1,50 @@ function parseQueryString(queryString) { const queryParams = {}; - if (queryString.length === 0) { + + if (!queryString || queryString.length === 0) { return queryParams; } + + // Helper to decode '+' as spaces and percent-encoded characters + function decodeParam(str) { + return decodeURIComponent(str.replace(/\+/g, " ")); + } + + // Split by '&' to get raw key-value pairs const keyValuePairs = queryString.split("&"); for (const pair of keyValuePairs) { - const [key, value] = pair.split("="); - queryParams[key] = value; + // Ignore empty pairs caused by trailing or duplicate '&' (e.g. "a=1&&b=2&") + if (pair.length === 0) { + continue; + } + + let rawKey, rawValue; + const equalIndex = pair.indexOf("="); + + if (equalIndex === -1) { + // Key with no '=' (e.g., "key") -> value is empty string + rawKey = pair; + rawValue = ""; + } else { + // Split on the FIRST '=' only (handles values containing '=', e.g. "a=b-2") + rawKey = pair.slice(0, equalIndex); + rawValue = pair.slice(equalIndex + 1); + } + + const key = decodeParam(rawKey); + const value = decodeParam(rawValue); + + // Stretch Goal: Handle duplicate keys by converting to an array + if (Object.prototype.hasOwnProperty.call(queryParams, key)) { + if (Array.isArray(queryParams[key])) { + queryParams[key].push(value); + } else { + queryParams[key] = [queryParams[key], value]; + } + } else { + queryParams[key] = value; + } } return queryParams; From 78a88c69b3f7047e2902af83d37a8f910a2c3c2e Mon Sep 17 00:00:00 2001 From: cywong Date: Mon, 3 Aug 2026 13:51:00 +0100 Subject: [PATCH 09/16] Add tests for duplicate keys in query string parsing Added tests for handling duplicate keys in query strings. --- Sprint-2/implement/querystring.test.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Sprint-2/implement/querystring.test.js b/Sprint-2/implement/querystring.test.js index 328b8df61..962ecf8a7 100644 --- a/Sprint-2/implement/querystring.test.js +++ b/Sprint-2/implement/querystring.test.js @@ -38,6 +38,12 @@ test("should replace '+' by ' '", () => { }); // Stretch exercise: Handling query strings that contain identical keys +test("should handle multiple duplicate keys alongside single keys", () => { + expect(parseQueryString("tag=js&tag=node&author=CYF")).toEqual({ + tag: ["js", "node"], + author: "CYF", + }); +}); // Delete this test if you are not working on this optional case test("should store values of a key in an array when the key has 2 or more values", () => { From 2d7c3ad688bf564eeaeefab5dfd4f328b29b2377 Mon Sep 17 00:00:00 2001 From: cywong Date: Mon, 3 Aug 2026 13:54:52 +0100 Subject: [PATCH 10/16] Implement tally function with input validation Added input validation and tallying functionality. --- Sprint-2/implement/tally.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/Sprint-2/implement/tally.js b/Sprint-2/implement/tally.js index f47321812..16c800576 100644 --- a/Sprint-2/implement/tally.js +++ b/Sprint-2/implement/tally.js @@ -1,3 +1,12 @@ -function tally() {} +function tally(items) { + if (!Array.isArray(items)) { + throw new TypeError("Expected an array as input"); + } + + return items.reduce((acc, item) => { + acc[item] = (acc[item] || 0) + 1; + return acc; + }, {}); +} module.exports = tally; From 720be29c6cd56359c9158fa4e3f9b844f87d576b Mon Sep 17 00:00:00 2001 From: cywong Date: Mon, 3 Aug 2026 13:58:49 +0100 Subject: [PATCH 11/16] Update tally.test.js --- Sprint-2/implement/tally.test.js | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/Sprint-2/implement/tally.test.js b/Sprint-2/implement/tally.test.js index 2ceffa8dd..ced8a644e 100644 --- a/Sprint-2/implement/tally.test.js +++ b/Sprint-2/implement/tally.test.js @@ -19,16 +19,30 @@ 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("returns counts for each unique item", () => { + expect(tally(["a"])).toEqual({ a: 1 }); + expect(tally(["a", "b", "c"])).toEqual({ a: 1, 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 duplicate items // When passed to tally // Then it should return counts for each unique item +test("returns counts for each unique item", () => { + expect(tally(["a", "a", "a"])).toEqual({ a: 3 }); + 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("throws an error when passed an invalid input like a string", () => { + expect(() => tally("string")).toThrow(TypeError); + expect(() => tally("string")).toThrow("Expected an array as input"); + }); From e6ff252e2284b1894236a4c8920c39fa90389e19 Mon Sep 17 00:00:00 2001 From: cywong Date: Mon, 3 Aug 2026 14:01:53 +0100 Subject: [PATCH 12/16] Add test cases for existing property names in contains --- Sprint-2/implement/contains.test.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Sprint-2/implement/contains.test.js b/Sprint-2/implement/contains.test.js index 49b9531ba..2daa43068 100644 --- a/Sprint-2/implement/contains.test.js +++ b/Sprint-2/implement/contains.test.js @@ -29,6 +29,8 @@ as the object doesn't contains a key of 'c' // Then it should return true test("returns true when passed an existing property name", () => { const inputObj = { a: 1, b: 2 }; + expect(contains(inputObj, "a")).toBe(true); + expect(contains(inputObj, "b")).toBe(true); }); // Given an object with properties From 427c69ca503913b2a84ae96e57bd46615c57604f Mon Sep 17 00:00:00 2001 From: cywong Date: Mon, 3 Aug 2026 14:14:59 +0100 Subject: [PATCH 13/16] Correct implementation of invert function Fix invert function to correctly invert object key-value pairs. --- Sprint-2/interpret/invert.js | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/Sprint-2/interpret/invert.js b/Sprint-2/interpret/invert.js index bb353fb1f..1780b9993 100644 --- a/Sprint-2/interpret/invert.js +++ b/Sprint-2/interpret/invert.js @@ -17,13 +17,29 @@ function invert(obj) { } // a) What is the current return value when invert is called with { a : 1 } - +{ key: 1 } // b) What is the current return value when invert is called with { a: 1, b: 2 } - +{ key: 2 } // c) What is the target return value when invert is called with {a : 1, b: 2} - +{ "1": "a", "2": "b" } // c) What does Object.entries return? Why is it needed in this program? - +Object.entries(obj) returns an array of key-value pairs as two-element arrays. +Object.entries({ a: 1, b: 2 }) returns [["a", 1], ["b", 2]]. // d) Explain why the current return value is different from the target output - +The line invertedObj.key = value; contains bugs: // e) Fix the implementation of invert (and write tests to prove it's fixed!) +function invert(obj) { + if (typeof obj !== "object" || obj === null || Array.isArray(obj)) { + throw new TypeError("Expected a plain object"); + } + + const invertedObj = {}; + + for (const [key, value] of Object.entries(obj)) { + invertedObj[value] = key; + } + + return invertedObj; +} + +module.exports = invert; From 978200ea42bdebddd05abf159ff22b025491615b Mon Sep 17 00:00:00 2001 From: cywong Date: Mon, 3 Aug 2026 14:16:34 +0100 Subject: [PATCH 14/16] Enhance countWords function with input checks Add input validation and handle empty strings in countWords function. --- Sprint-2/stretch/count-words.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/Sprint-2/stretch/count-words.js b/Sprint-2/stretch/count-words.js index 8e85d19d7..811033859 100644 --- a/Sprint-2/stretch/count-words.js +++ b/Sprint-2/stretch/count-words.js @@ -26,3 +26,27 @@ 3. Order the results to find out which word is the most common in the input */ + +function countWords(str) { + if (typeof str !== "string") { + throw new TypeError("Expected a string as input"); + } + + // Handle empty or whitespace-only strings + if (str.trim() === "") return {}; + + const words = str.split(" "); + const counts = {}; + + for (const word of words) { + if (counts[word]) { + counts[word] += 1; + } else { + counts[word] = 1; + } + } + + return counts; +} + +module.exports = countWords; From 3dfb2a8694dba8e8b9fe0ab39dc7e6bfa8c11ad5 Mon Sep 17 00:00:00 2001 From: cywong Date: Mon, 3 Aug 2026 14:20:22 +0100 Subject: [PATCH 15/16] Refactor calculateMode into smaller functions --- Sprint-2/stretch/mode.js | 41 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/Sprint-2/stretch/mode.js b/Sprint-2/stretch/mode.js index 3f7609d79..b7bc5138b 100644 --- a/Sprint-2/stretch/mode.js +++ b/Sprint-2/stretch/mode.js @@ -8,6 +8,7 @@ // refactor calculateMode by splitting up the code // into smaller functions using the stages above +/* function calculateMode(list) { // track frequency of each value let freqs = new Map(); @@ -34,3 +35,43 @@ function calculateMode(list) { } module.exports = calculateMode; +*/ + +function getFrequencies(list) { + const freqs = new Map(); + + for (const num of list) { + if (typeof num !== "number") { + continue; + } + freqs.set(num, (freqs.get(num) || 0) + 1); + } + + return freqs; +} + + +function getHighestFrequencyKey(freqs) { + let maxFreq = 0; + let mode; + + for (const [num, freq] of freqs) { + if (freq > maxFreq) { + mode = num; + maxFreq = freq; + } + } + + return maxFreq === 0 ? NaN : mode; +} + +function calculateMode(list) { + const freqs = getFrequencies(list); + return getHighestFrequencyKey(freqs); +} + +module.exports = { + calculateMode, + getFrequencies, + getHighestFrequencyKey, +}; From f204ada34d1789660eb6028a0f59b070c7d0c11c Mon Sep 17 00:00:00 2001 From: cywong Date: Mon, 3 Aug 2026 14:27:51 +0100 Subject: [PATCH 16/16] Fix totalTill implementation and add test case Fixed the totalTill function implementation to correctly calculate the total amount in pounds from the till object. Added a test case to verify the functionality. --- Sprint-2/stretch/till.js | 38 +++++++++++++++++++++++++++++++++++--- 1 file changed, 35 insertions(+), 3 deletions(-) diff --git a/Sprint-2/stretch/till.js b/Sprint-2/stretch/till.js index 6a08532e7..d52e37433 100644 --- a/Sprint-2/stretch/till.js +++ b/Sprint-2/stretch/till.js @@ -23,9 +23,41 @@ const till = { const totalAmount = totalTill(till); // a) What is the target output when totalTill is called with the till object - +"£4.40" // b) Why do we need to use Object.entries inside the for...of loop in this function? - +We need it because plain objects are not directly iterable using a for...of loop in JavaScript. Using Object.entries allows us to loop through the entries // c) What does coin * quantity evaluate to inside the for...of loop? - +coin * quantity evaluates to NaN (Not-a-Number). // d) Write a test for this function to check it works and then fix the implementation of totalTill + function totalTill(till) { + let totalPence = 0; + + for (const [coin, quantity] of Object.entries(till)) { + // Parse the numeric pence value from strings like "50p" -> 50 + const penceValue = parseInt(coin, 10); + + if (!isNaN(penceValue)) { + totalPence += penceValue * quantity; + } + } + + // Convert pence to pounds and format to 2 decimal places + const pounds = (totalPence / 100).toFixed(2); + return `£${pounds}`; +} + +module.exports = totalTill; + +const totalTill = require("./totalTill.js"); + +describe("totalTill", () => { + test("calculates the correct total for a given till object", () => { + const till = { + "1p": 10, + "5p": 6, + "50p": 4, + "20p": 10, + }; + expect(totalTill(till)).toBe("£4.40"); + }); +});