From 4b1481f69f16e3de170fcdeac6d3ded17982cfdd Mon Sep 17 00:00:00 2001 From: MacDonald91 Date: Thu, 12 Mar 2026 14:45:30 +0000 Subject: [PATCH 1/2] Complete Sprint 2 exercises --- Sprint-2/debug/address.js | 4 +-- Sprint-2/debug/author.js | 4 +-- Sprint-2/debug/recipe.js | 8 ++++-- Sprint-2/implement/contains.js | 12 +++++++-- Sprint-2/implement/contains.test.js | 34 ++++++++++++-------------- Sprint-2/implement/lookup.js | 15 +++++++++--- Sprint-2/implement/lookup.test.js | 18 ++++++++++++-- Sprint-2/implement/querystring.js | 18 ++++++++++++-- Sprint-2/implement/querystring.test.js | 25 ++++++++++++++++++- Sprint-2/implement/tally.js | 24 ++++++++++++++++-- Sprint-2/implement/tally.test.js | 19 +++++++++++++- Sprint-2/interpret/invert.js | 19 +++++++++++--- Sprint-2/package-lock.json | 2 ++ 13 files changed, 160 insertions(+), 42 deletions(-) diff --git a/Sprint-2/debug/address.js b/Sprint-2/debug/address.js index 940a6af83..e8480849b 100644 --- a/Sprint-2/debug/address.js +++ b/Sprint-2/debug/address.js @@ -1,5 +1,5 @@ // Predict and explain first... - +// The address variable is an object literal containing key–value pairs separated by commas. The code originally tries to access address[0], which would only work if the data were in an array. Since address is an object and not an array, there is no index 0, so it returns undefined. To fix the problem we must access the property using its key, for example 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 +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..5378a9fa9 100644 --- a/Sprint-2/debug/author.js +++ b/Sprint-2/debug/author.js @@ -1,5 +1,5 @@ // Predict and explain first... - +// The program does not work because for...of can only be used on iterable objects such as arrays or strings. The author variable is an object, which is not iterable by default. To fix the problem we can use Object.values(author) to convert the object values into an iterable array and then loop through them. // 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 +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..31d5f23cb 100644 --- a/Sprint-2/debug/recipe.js +++ b/Sprint-2/debug/recipe.js @@ -1,5 +1,5 @@ // Predict and explain first... - +// The code does not work because ${recipe} prints the entire object rather than the ingredients array. To display each ingredient on a new line, we need to loop through recipe.ingredients, which is an array. Using a for...of loop allows us to print each ingredient individually. // 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? @@ -11,5 +11,9 @@ const recipe = { }; console.log(`${recipe.title} serves ${recipe.serves} - ingredients: + ("ingredients:") ${recipe}`); + +for (const ingredient of recipe.ingredients) { + console.log(ingredient); +} \ No newline at end of file diff --git a/Sprint-2/implement/contains.js b/Sprint-2/implement/contains.js index cd779308a..e644bc7d5 100644 --- a/Sprint-2/implement/contains.js +++ b/Sprint-2/implement/contains.js @@ -1,3 +1,11 @@ -function contains() {} +function contains(object, property) { -module.exports = contains; + // check if parameter is a valid object and not an array + if (typeof object !== "object" || Array.isArray(object) || object === null) { + return false; + } + + return Object.prototype.hasOwnProperty.call(object, property); +} + +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 326bdb1f2..07cd60a82 100644 --- a/Sprint-2/implement/contains.test.js +++ b/Sprint-2/implement/contains.test.js @@ -1,35 +1,31 @@ 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 - // 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("returns true when object contains the property", () => { + const obj = { a: 1, b: 2 }; + expect(contains(obj, "a")).toBe(true); +}); // Given an object with properties // When passed to contains with a non-existent property name // Then it should return false +test("returns false when object does not contain the property", () => { + const obj = { a: 1, b: 2 }; + expect(contains(obj, "c")).toBe(false); +}); // Given invalid parameters like an array // When passed to contains -// Then it should return false or throw an error +// Then it should return false +test("returns false when passed invalid parameters like an array", () => { + expect(contains([1, 2, 3], "a")).toBe(false); +}); \ No newline at end of file diff --git a/Sprint-2/implement/lookup.js b/Sprint-2/implement/lookup.js index a6746e07f..4cd8ad608 100644 --- a/Sprint-2/implement/lookup.js +++ b/Sprint-2/implement/lookup.js @@ -1,5 +1,14 @@ -function createLookup() { - // implementation here +function createLookup(countryCurrencyPairs) { + const lookup = {}; + + for (const pair of countryCurrencyPairs) { + const countryCode = pair[0]; + const currencyCode = pair[1]; + + lookup[countryCode] = currencyCode; + } + + return lookup; } -module.exports = createLookup; +module.exports = createLookup; \ No newline at end of file diff --git a/Sprint-2/implement/lookup.test.js b/Sprint-2/implement/lookup.test.js index 547e06c5a..26d786600 100644 --- a/Sprint-2/implement/lookup.test.js +++ b/Sprint-2/implement/lookup.test.js @@ -1,6 +1,20 @@ 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 countryCurrencyPairs = [ + ["US", "USD"], + ["CA", "CAD"] + ]; + + const result = createLookup(countryCurrencyPairs); + + expect(result).toEqual({ + US: "USD", + CA: "CAD" + }); + +}); /* @@ -32,4 +46,4 @@ It should return: 'US': 'USD', 'CA': 'CAD' } -*/ +*/ \ No newline at end of file diff --git a/Sprint-2/implement/querystring.js b/Sprint-2/implement/querystring.js index 45ec4e5f3..349b73680 100644 --- a/Sprint-2/implement/querystring.js +++ b/Sprint-2/implement/querystring.js @@ -1,16 +1,30 @@ function parseQueryString(queryString) { const queryParams = {}; + + // If the query string is empty return empty object if (queryString.length === 0) { return queryParams; } + const keyValuePairs = queryString.split("&"); for (const pair of keyValuePairs) { - const [key, value] = pair.split("="); + + const index = pair.indexOf("="); + + // If no "=" exists treat the value as empty + if (index === -1) { + queryParams[pair] = ""; + continue; + } + + const key = pair.slice(0, index); + const value = pair.slice(index + 1); + queryParams[key] = value; } return queryParams; } -module.exports = parseQueryString; +module.exports = parseQueryString; \ No newline at end of file diff --git a/Sprint-2/implement/querystring.test.js b/Sprint-2/implement/querystring.test.js index 3e218b789..7c35084ec 100644 --- a/Sprint-2/implement/querystring.test.js +++ b/Sprint-2/implement/querystring.test.js @@ -7,6 +7,29 @@ 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("returns empty object for empty query string", () => { + expect(parseQueryString("")).toEqual({}); +}); + +test("parses multiple parameters", () => { + expect(parseQueryString("name=John&age=30")).toEqual({ + name: "John", + age: "30", + }); +}); + +test("handles parameter with empty value", () => { + expect(parseQueryString("name=")).toEqual({ + name: "", + }); +}); + +test("handles parameter without equals sign", () => { + expect(parseQueryString("name")).toEqual({ + name: "", + }); +}); \ No newline at end of file diff --git a/Sprint-2/implement/tally.js b/Sprint-2/implement/tally.js index f47321812..748eb007a 100644 --- a/Sprint-2/implement/tally.js +++ b/Sprint-2/implement/tally.js @@ -1,3 +1,23 @@ -function tally() {} +function tally(items) { -module.exports = tally; + if (!Array.isArray(items)) { + throw new Error("Input must be an array"); + } + + const result = {}; + + for (const item of items) { + + if (result[item]) { + result[item] += 1; + } else { + result[item] = 1; + } + + } + + return result; + +} + +module.exports = tally; \ No newline at end of file diff --git a/Sprint-2/implement/tally.test.js b/Sprint-2/implement/tally.test.js index 2ceffa8dd..0da04e41f 100644 --- a/Sprint-2/implement/tally.test.js +++ b/Sprint-2/implement/tally.test.js @@ -19,16 +19,33 @@ 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("counts frequency of items in an array", () => { + 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 duplicate items // When passed to tally // Then it should return counts for each unique item +test("counts duplicates 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("throws error if input is not an array", () => { + 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..81e1e1d7a 100644 --- a/Sprint-2/interpret/invert.js +++ b/Sprint-2/interpret/invert.js @@ -10,20 +10,31 @@ function invert(obj) { const invertedObj = {}; for (const [key, value] of Object.entries(obj)) { - invertedObj.key = value; + // swap key and value + invertedObj[value] = key; } return invertedObj; } // a) What is the current return value when invert is called with { a : 1 } +// Before fixing the code it returned: { key: 1 } // b) What is the current return value when invert is called with { a: 1, b: 2 } +// Before fixing the code it returned: { 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? +// d) What does Object.entries return? Why is it needed in this program? +// Object.entries(obj) returns an array of [key, value] pairs. +// Example: Object.entries({a:1, b:2}) +// returns: [["a",1], ["b",2]] +// It allows us to loop through both keys and values of an object. -// d) Explain why the current return value is different from the target output +// e) Explain why the current return value is different from the target output +// The bug was that the code used invertedObj.key which creates a property +// literally called "key". Instead we need to use the variable value as the key, +// so we use bracket notation: invertedObj[value] = key. -// e) Fix the implementation of invert (and write tests to prove it's fixed!) +module.exports = invert; \ No newline at end of file diff --git a/Sprint-2/package-lock.json b/Sprint-2/package-lock.json index 9b4c725d6..ceda7296e 100644 --- a/Sprint-2/package-lock.json +++ b/Sprint-2/package-lock.json @@ -56,6 +56,7 @@ "integrity": "sha512-Oixnb+DzmRT30qu9d3tJSQkxuygWm32DFykT4bRoORPa9hZ/L4KhVB/XiRm6KG+roIEM7DBQlmg27kw2HZkdZg==", "dev": true, "license": "MIT", + "peer": true, "dependencies": { "@ampproject/remapping": "^2.2.0", "@babel/code-frame": "^7.25.7", @@ -1368,6 +1369,7 @@ } ], "license": "MIT", + "peer": true, "dependencies": { "caniuse-lite": "^1.0.30001663", "electron-to-chromium": "^1.5.28", From c100c936e8e57f83855be623a709d978ecb1a889 Mon Sep 17 00:00:00 2001 From: MacDonald91 Date: Wed, 15 Apr 2026 17:58:04 +0100 Subject: [PATCH 2/2] Fix: remove unnecessary parentheses and clean recipe output --- Sprint-2/debug/recipe.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Sprint-2/debug/recipe.js b/Sprint-2/debug/recipe.js index 31d5f23cb..1ff7a335a 100644 --- a/Sprint-2/debug/recipe.js +++ b/Sprint-2/debug/recipe.js @@ -11,8 +11,7 @@ const recipe = { }; console.log(`${recipe.title} serves ${recipe.serves} - ("ingredients:") -${recipe}`); +Ingredients:`); for (const ingredient of recipe.ingredients) { console.log(ingredient);