From 75e41011a371368baa88361aacb5155b281fdb6d Mon Sep 17 00:00:00 2001 From: anitahy73 Date: Thu, 6 Aug 2026 19:01:02 +0100 Subject: [PATCH 01/12] Fix console log to access houseNumber property --- 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 8eb363ec7ac383b4508282147e42e05066c92af1 Mon Sep 17 00:00:00 2001 From: anitahy73 Date: Thu, 6 Aug 2026 19:02:41 +0100 Subject: [PATCH 02/12] Fix iteration over author object using Object.values --- Sprint-2/debug/author.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Sprint-2/debug/author.js b/Sprint-2/debug/author.js index 8c2125977..988c5e207 100644 --- a/Sprint-2/debug/author.js +++ b/Sprint-2/debug/author.js @@ -1,4 +1,6 @@ // Predict and explain first... +// The author variable is an object. As such, it is not iterable by default. +// Therefore, using a for...of loop on it throws a TypeError. // 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 +13,6 @@ const author = { alive: true, }; -for (const value of author) { +for (const value of Object.values(author)) { console.log(value); } From 2f702f0f2c27462b24ac8bbd942cfee26f645881 Mon Sep 17 00:00:00 2001 From: anitahy73 Date: Thu, 6 Aug 2026 19:03:16 +0100 Subject: [PATCH 03/12] Fix recipe logging to show ingredients correctly Updated recipe logging to display ingredients on separate lines. --- Sprint-2/debug/recipe.js | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/Sprint-2/debug/recipe.js b/Sprint-2/debug/recipe.js index 6cbdd22cd..bbd8ee11f 100644 --- a/Sprint-2/debug/recipe.js +++ b/Sprint-2/debug/recipe.js @@ -1,7 +1,10 @@ // Predict and explain first... +// The recipe object is interpolated into the template literal (${recipe}). +// During interpolation, JavaScript converts the object to a string. +// Consequently, the object's properties, including the ingredients array, is not displayed. // This program should log out the title, how many it serves and the ingredients. -// Each ingredient should be logged on a new line +// Each ingredient should be logged on a new line. // How can you fix it? const recipe = { @@ -10,6 +13,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 ingredient of recipe.ingredients) { + console.log(ingredient); +} From 44fd8312c3c46996702280cd3a84597514e6ee73 Mon Sep 17 00:00:00 2001 From: anitahy73 Date: Thu, 6 Aug 2026 19:03:52 +0100 Subject: [PATCH 04/12] Implement contains function to check object properties --- Sprint-2/implement/contains.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Sprint-2/implement/contains.js b/Sprint-2/implement/contains.js index cd779308a..12b55a181 100644 --- a/Sprint-2/implement/contains.js +++ b/Sprint-2/implement/contains.js @@ -1,3 +1,8 @@ -function contains() {} +function contains(obj, property) { + if (obj === null || typeof obj !== "object" || Array.isArray(obj)) { + return false; + } + return property in obj; +} module.exports = contains; From 37a2b47af0eb0fc23ff8cd1ceb96792239610ecf Mon Sep 17 00:00:00 2001 From: anitahy73 Date: Thu, 6 Aug 2026 19:04:19 +0100 Subject: [PATCH 05/12] Implement createLookup function in lookup.js Implemented the createLookup function to build a lookup object from country-currency pairs. --- Sprint-2/implement/lookup.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Sprint-2/implement/lookup.js b/Sprint-2/implement/lookup.js index a6746e07f..ea5ca8432 100644 --- a/Sprint-2/implement/lookup.js +++ b/Sprint-2/implement/lookup.js @@ -1,5 +1,11 @@ function createLookup() { - // implementation here + const lookup = {}; + + countryCurrencyPairs.forEach(pair => { + lookup[pair[0]] = pair[1]; + }); + + return lookup; } module.exports = createLookup; From a169848ca243ed41469874b432c8c084691ba6aa Mon Sep 17 00:00:00 2001 From: anitahy73 Date: Thu, 6 Aug 2026 19:05:11 +0100 Subject: [PATCH 06/12] Enhance query string parsing functionality Refactor parseQueryString to handle empty pairs and decode keys/values. --- Sprint-2/implement/querystring.js | 40 +++++++++++++++++++++++++++---- 1 file changed, 35 insertions(+), 5 deletions(-) diff --git a/Sprint-2/implement/querystring.js b/Sprint-2/implement/querystring.js index 45ec4e5f3..e5363e745 100644 --- a/Sprint-2/implement/querystring.js +++ b/Sprint-2/implement/querystring.js @@ -1,13 +1,43 @@ function parseQueryString(queryString) { const queryParams = {}; - if (queryString.length === 0) { + + if (queryString === "") { return queryParams; } - const keyValuePairs = queryString.split("&"); - for (const pair of keyValuePairs) { - const [key, value] = pair.split("="); - queryParams[key] = value; + const pairs = queryString.split("&"); + + for (const pair of pairs) { + if (pair === "") { + continue; + } + + const equalIndex = pair.indexOf("="); + + let key; + let value; + + if (equalIndex === -1) { + key = pair; + value = ""; + } else { + key = pair.slice(0, equalIndex); + value = pair.slice(equalIndex + 1); + } + + key = key.replace(/\+/g, " "); + value = value.replace(/\+/g, " "); + + key = decodeURIComponent(key); + value = decodeURIComponent(value); + + if (queryParams[key] === undefined) { + queryParams[key] = value; + } else if (Array.isArray(queryParams[key])) { + queryParams[key].push(value); + } else { + queryParams[key] = [queryParams[key], value]; + } } return queryParams; From 7c4bfb97965ccce15ee6cb306fc9846bfa2945c7 Mon Sep 17 00:00:00 2001 From: anitahy73 Date: Thu, 6 Aug 2026 19:05:52 +0100 Subject: [PATCH 07/12] Remove stretch exercise test for identical keys Removed optional test for handling identical keys in query strings. --- Sprint-2/implement/querystring.test.js | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/Sprint-2/implement/querystring.test.js b/Sprint-2/implement/querystring.test.js index 328b8df61..8ce9d4eb6 100644 --- a/Sprint-2/implement/querystring.test.js +++ b/Sprint-2/implement/querystring.test.js @@ -36,13 +36,3 @@ test("should replace '+' by ' '", () => { "full name": "John Doe", }); }); - -// Stretch exercise: Handling query strings that contain identical keys - -// 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", () => { - expect(parseQueryString("key=value1&key=value2&key=value3&foo=bar")).toEqual({ - key: ["value1", "value2", "value3"], - foo: "bar", - }); -}); From 455d28dea163ab2b59970d38ead24866ab9c2a05 Mon Sep 17 00:00:00 2001 From: anitahy73 Date: Thu, 6 Aug 2026 19:06:30 +0100 Subject: [PATCH 08/12] Implement tally function to count item occurrences --- Sprint-2/implement/tally.js | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/Sprint-2/implement/tally.js b/Sprint-2/implement/tally.js index f47321812..ad874706c 100644 --- a/Sprint-2/implement/tally.js +++ b/Sprint-2/implement/tally.js @@ -1,3 +1,15 @@ -function tally() {} +function tally() { + const counts = {}; + + for (const item of items) { + if (counts[item] === undefined) { + counts[item] = 1; + } else { + counts[item]++; + } + } + + return counts; +} module.exports = tally; From 27d42f08247e535e99603dd12254e7ad3ab7c3f5 Mon Sep 17 00:00:00 2001 From: anitahy73 Date: Thu, 6 Aug 2026 19:09:11 +0100 Subject: [PATCH 09/12] Fix invert function to correctly swap keys and values --- Sprint-2/interpret/invert.js | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/Sprint-2/interpret/invert.js b/Sprint-2/interpret/invert.js index bb353fb1f..d5c960d8d 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; + invertedObj[value] = key; } return invertedObj; } -// a) What is the current return value when invert is called with { a : 1 } +console.log("invert.js loaded"); -// b) What is the current return value when invert is called with { a: 1, b: 2 } +module.exports = invert; -// c) What is the target return value when invert is called with {a : 1, b: 2} +// 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}. -// c) What does Object.entries return? Why is it needed in this program? +// 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}. -// d) Explain why the current return value is different from the target output +// 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}. -// e) Fix the implementation of invert (and write tests to prove it's fixed!) +// d) What does Object.entries return? Why is it needed in this program? +// Object.entries() returns an array containing the key-value pairs of an object. +// It is needed in this program because the function must access both the keys and the values in order to swap them. + +// e) Explain why the current return value is different from the target output. +// The current return value is different from the target output because invertedObj.key = value does not use the variable key as the property name. +// Bracket notation is needed to swap the keys and values. + +// f) Fix the implementation of invert (and write tests to prove it's fixed!) From 037bacdac6dea4ff580874d2c1795337f2d0ebcd Mon Sep 17 00:00:00 2001 From: anitahy73 Date: Thu, 6 Aug 2026 19:09:56 +0100 Subject: [PATCH 10/12] Add files via upload --- Sprint-2/interpret/invert.test.js | 18 ++++++++++++++++++ 1 file changed, 18 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..89b537301 --- /dev/null +++ b/Sprint-2/interpret/invert.test.js @@ -0,0 +1,18 @@ +const invert = require("./invert.js"); + +test("should swap keys and values in an object", () => { + expect(invert({ a: 1, b: 2 })).toEqual({ + 1: "a", + 2: "b", + }); +}); + +test("should invert an object with a single key-value pair", () => { + expect(invert({ x: 10 })).toEqual({ + 10: "x", + }); +}); + +test("should return an empty object when passed an empty object", () => { + expect(invert({})).toEqual({}); +}); \ No newline at end of file From 5ad7f86296e01df4b3382cfddaa4e0b83064c60a Mon Sep 17 00:00:00 2001 From: anitahy73 Date: Thu, 6 Aug 2026 19:11:11 +0100 Subject: [PATCH 11/12] Implement invert function and add tests --- Sprint-2/interpret/invert.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sprint-2/interpret/invert.test.js b/Sprint-2/interpret/invert.test.js index 89b537301..492bc676d 100644 --- a/Sprint-2/interpret/invert.test.js +++ b/Sprint-2/interpret/invert.test.js @@ -15,4 +15,4 @@ test("should invert an object with a single key-value pair", () => { test("should return an empty object when passed an empty object", () => { expect(invert({})).toEqual({}); -}); \ No newline at end of file +}); From 9c42c429c741fe4a07ad85f723872063245d5fb6 Mon Sep 17 00:00:00 2001 From: anitahy73 Date: Thu, 6 Aug 2026 19:53:51 +0100 Subject: [PATCH 12/12] Add instructions --- readme.md | 1 + 1 file changed, 1 insertion(+) diff --git a/readme.md b/readme.md index b9d0c065e..b6bd638f9 100644 --- a/readme.md +++ b/readme.md @@ -17,3 +17,4 @@ There are some tools that will help you to write code. One of these, [Prettier]( - In Visual Studio open the settings file (see https://code.visualstudio.com/docs/getstarted/settings#_creating-user-and-workspace-settings) - Search for `editor format` - Set `editor.formatOnSave` and `editor.formatOnPaste` to true +-