From 6c481d8ddd36dacc74f360c25750912d49ee38e5 Mon Sep 17 00:00:00 2001 From: Francesco Romano Monda Date: Wed, 29 Jul 2026 18:36:41 +0100 Subject: [PATCH 1/4] Resolve debugging tasks --- Sprint-2/debug/address.js | 4 +++- Sprint-2/debug/author.js | 4 +++- Sprint-2/debug/recipe.js | 14 ++++++++++---- 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/Sprint-2/debug/address.js b/Sprint-2/debug/address.js index 940a6af83..4de601e80 100644 --- a/Sprint-2/debug/address.js +++ b/Sprint-2/debug/address.js @@ -1,4 +1,6 @@ // Predict and explain first... +// address is an object, not an array. +// Because it does not have a property named "0", this code will log: "My house number is undefined". // This code should log out the houseNumber from the address object // but it 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..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); } diff --git a/Sprint-2/debug/recipe.js b/Sprint-2/debug/recipe.js index 6cbdd22cd..0811c108d 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); +} \ No newline at end of file From 4c6a9e4fe30a609363b829c2d4c081f5b25555ad Mon Sep 17 00:00:00 2001 From: Francesco Romano Monda Date: Wed, 29 Jul 2026 21:50:23 +0100 Subject: [PATCH 2/4] Implement contains and lookup functions --- Sprint-2/implement/contains.js | 12 +++++++++++- Sprint-2/implement/lookup.js | 8 +++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/Sprint-2/implement/contains.js b/Sprint-2/implement/contains.js index cd779308a..ccf7051b6 100644 --- a/Sprint-2/implement/contains.js +++ b/Sprint-2/implement/contains.js @@ -1,3 +1,13 @@ -function contains() {} +function contains(obj, property) { + if ( + obj === null || + typeof obj !== "object" || + Array.isArray(obj) + ) { + return false; +} +} + +return property in obj; module.exports = contains; 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 7dd570062a0eb8156e96fe4a5ed9e4d3713a888e Mon Sep 17 00:00:00 2001 From: Francesco Romano Monda Date: Thu, 30 Jul 2026 20:28:29 +0100 Subject: [PATCH 3/4] Finish implement exercises --- Sprint-2/implement/contains.js | 11 ++----- Sprint-2/implement/querystring.js | 40 ++++++++++++++++++++++---- Sprint-2/implement/querystring.test.js | 10 ------- Sprint-2/implement/tally.js | 14 ++++++++- Sprint-2/interpret/invert.js | 21 +------------- 5 files changed, 52 insertions(+), 44 deletions(-) diff --git a/Sprint-2/implement/contains.js b/Sprint-2/implement/contains.js index ccf7051b6..12b55a181 100644 --- a/Sprint-2/implement/contains.js +++ b/Sprint-2/implement/contains.js @@ -1,13 +1,8 @@ function contains(obj, property) { - if ( - obj === null || - typeof obj !== "object" || - Array.isArray(obj) - ) { + if (obj === null || typeof obj !== "object" || Array.isArray(obj)) { return false; + } + return property in obj; } -} - -return property in obj; module.exports = contains; 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; 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", - }); -}); 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; diff --git a/Sprint-2/interpret/invert.js b/Sprint-2/interpret/invert.js index bb353fb1f..012b3ca99 100644 --- a/Sprint-2/interpret/invert.js +++ b/Sprint-2/interpret/invert.js @@ -1,23 +1,4 @@ -// Let's define how invert should work - -// Given an object -// When invert is passed this object -// Then it should swap the keys and values in the object - -// E.g. invert({x : 10, y : 20}), target output: {"10": "x", "20": "y"} - -function invert(obj) { - const invertedObj = {}; - - for (const [key, value] of Object.entries(obj)) { - invertedObj.key = value; - } - - return invertedObj; -} - -// a) What is the current return value when invert is called with { a : 1 } - +// // b) What is the current return value when invert is called with { a: 1, b: 2 } // c) What is the target return value when invert is called with {a : 1, b: 2} From 9be0a69146e921f4dde28dfcb8c5145dfc5ddd38 Mon Sep 17 00:00:00 2001 From: Francesco Romano Monda Date: Thu, 30 Jul 2026 21:44:36 +0100 Subject: [PATCH 4/4] Implement invert function and add tests --- Sprint-2/interpret/invert.js | 42 ++++++++++++++++++++++++++----- Sprint-2/interpret/invert.test.js | 18 +++++++++++++ 2 files changed, 54 insertions(+), 6 deletions(-) create mode 100644 Sprint-2/interpret/invert.test.js diff --git a/Sprint-2/interpret/invert.js b/Sprint-2/interpret/invert.js index 012b3ca99..d5c960d8d 100644 --- a/Sprint-2/interpret/invert.js +++ b/Sprint-2/interpret/invert.js @@ -1,10 +1,40 @@ -// -// b) What is the current return value when invert is called with { a: 1, b: 2 } +// Let's define how invert should work -// c) What is the target return value when invert is called with {a : 1, b: 2} +// Given an object +// When invert is passed this object +// Then it should swap the keys and values in the object -// c) What does Object.entries return? Why is it needed in this program? +// E.g. invert({x : 10, y : 20}), target output: {"10": "x", "20": "y"} -// d) Explain why the current return value is different from the target output +function invert(obj) { + const invertedObj = {}; -// e) Fix the implementation of invert (and write tests to prove it's fixed!) + for (const [key, value] of Object.entries(obj)) { + invertedObj[value] = key; + } + + return invertedObj; +} + +console.log("invert.js loaded"); + +module.exports = invert; + +// 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}. + +// 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!) 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