From fd0a7dafb58a32f64e6faf760e8a003cc033b1cc Mon Sep 17 00:00:00 2001 From: shafiekwalker7861 Date: Tue, 28 Jul 2026 09:39:26 +0200 Subject: [PATCH 1/3] Complete Sprint 2 debug and implement exercises --- Sprint-2/debug/address.js | 4 +++- Sprint-2/debug/author.js | 4 +++- Sprint-2/debug/recipe.js | 9 +++++++-- Sprint-2/implement/contains.js | 10 ++++++++-- Sprint-2/implement/contains.test.js | 14 +++++++++++++- Sprint-2/implement/lookup.js | 12 +++++++++--- Sprint-2/implement/lookup.test.js | 12 +++++++++++- Sprint-2/implement/querystring.js | 26 ++++++++++++++++++++++++-- Sprint-2/implement/tally.js | 16 ++++++++++++++-- Sprint-2/implement/tally.test.js | 16 +++++++++++++++- 10 files changed, 107 insertions(+), 16 deletions(-) diff --git a/Sprint-2/debug/address.js b/Sprint-2/debug/address.js index 940a6af83..30293e813 100644 --- a/Sprint-2/debug/address.js +++ b/Sprint-2/debug/address.js @@ -1,7 +1,9 @@ // Predict and explain first... // This code should log out the houseNumber from the address object + // but it isn't working... + // Fix anything that isn't working const address = { @@ -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..e46c75ed6 100644 --- a/Sprint-2/debug/author.js +++ b/Sprint-2/debug/author.js @@ -2,6 +2,8 @@ // 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 +// The problem is that the for...of loop is being used on an object, +// which is not iterable. Instead, we should use Object.values() to get an array of the object's values and then iterate over that array. const author = { firstName: "Zadie", @@ -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..2a62f2996 100644 --- a/Sprint-2/debug/recipe.js +++ b/Sprint-2/debug/recipe.js @@ -1,8 +1,11 @@ // Predict and explain first... +// // 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? +// The problem is that the recipe object is being logged directly, which will not display the ingredients in the desired format. +// Instead, we should iterate over the ingredients array and log each ingredient on a new line. const recipe = { title: "bruschetta", @@ -11,5 +14,7 @@ const recipe = { }; console.log(`${recipe.title} serves ${recipe.serves} - ingredients: -${recipe}`); + ingredients:`); +recipe.ingredients.forEach((ingredient) => { + console.log(`- ${ingredient}`); +}); diff --git a/Sprint-2/implement/contains.js b/Sprint-2/implement/contains.js index cd779308a..92764fa25 100644 --- a/Sprint-2/implement/contains.js +++ b/Sprint-2/implement/contains.js @@ -1,3 +1,9 @@ -function contains() {} +function contains(object, propertyName) { + if (Array.isArray(object)) { + return false; + } -module.exports = contains; + return Object.hasOwn(object, propertyName); +} + +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..ddb8585cd 100644 --- a/Sprint-2/implement/contains.test.js +++ b/Sprint-2/implement/contains.test.js @@ -20,16 +20,28 @@ as the object doesn't contains a key of 'c' // Given an empty object // When passed to contains // Then it should return false -test.todo("contains on empty object returns false"); +test("contains on an empty object returns false", () => { + expect(contains({}, "propertyName")).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 the property exists", () => { + expect(contains({ a: 1, b: 2 }, "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 the property doesn't exist", () => { + expect(contains({ a: 1, b: 2 }, "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 for invalid input types", () => { + expect(contains([1, 2, 3], "propertyName")).toBe(false); +}); \ No newline at end of file diff --git a/Sprint-2/implement/lookup.js b/Sprint-2/implement/lookup.js index a6746e07f..5d373c8d6 100644 --- a/Sprint-2/implement/lookup.js +++ b/Sprint-2/implement/lookup.js @@ -1,5 +1,11 @@ -function createLookup() { - // implementation here +function createLookup(countryCurrencyPairs) { + const lookup = {}; + + for (const [countryCode, currencyCode] of countryCurrencyPairs) { + 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..b3bd1c68d 100644 --- a/Sprint-2/implement/lookup.test.js +++ b/Sprint-2/implement/lookup.test.js @@ -1,6 +1,16 @@ 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"], + ]; + + expect(createLookup(countryCurrencyPairs)).toEqual({ + US: "USD", + CA: "CAD", + }); +}); /* diff --git a/Sprint-2/implement/querystring.js b/Sprint-2/implement/querystring.js index 45ec4e5f3..521c32acf 100644 --- a/Sprint-2/implement/querystring.js +++ b/Sprint-2/implement/querystring.js @@ -1,13 +1,35 @@ function parseQueryString(queryString) { const queryParams = {}; + if (queryString.length === 0) { return queryParams; } + const keyValuePairs = queryString.split("&"); for (const pair of keyValuePairs) { - const [key, value] = pair.split("="); - queryParams[key] = value; + if (pair === "") { + continue; + } + + const separatorIndex = pair.indexOf("="); + + const encodedKey = + separatorIndex === -1 ? pair : pair.slice(0, separatorIndex); + + const encodedValue = + separatorIndex === -1 ? "" : pair.slice(separatorIndex + 1); + + const key = decodeURIComponent(encodedKey.replaceAll("+", " ")); + const value = decodeURIComponent(encodedValue.replaceAll("+", " ")); + + if (!Object.hasOwn(queryParams, key)) { + 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/tally.js b/Sprint-2/implement/tally.js index f47321812..d7cc238bf 100644 --- a/Sprint-2/implement/tally.js +++ b/Sprint-2/implement/tally.js @@ -1,3 +1,15 @@ -function tally() {} +function tally(items) { + if (!Array.isArray(items)) { + throw new TypeError("Expected an array"); + } -module.exports = tally; + const counts = {}; + + for (const item of items) { + counts[item] = (counts[item] || 0) + 1; + } + + return counts; +} + +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..378a8706c 100644 --- a/Sprint-2/implement/tally.test.js +++ b/Sprint-2/implement/tally.test.js @@ -23,7 +23,21 @@ const tally = require("./tally.js"); // 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({}); +}); + +test("returns the count for each unique item", () => { + expect(tally(["a", "a", "b", "c"])).toEqual({ + a: 2, + b: 1, + c: 1, + }); +}); + +test("throws an error when passed a string", () => { + expect(() => tally("a, a, b")).toThrow(TypeError); +}); // Given an array with duplicate items // When passed to tally From f1c079887d08b7ce3907c9a3c81741b5082017dc Mon Sep 17 00:00:00 2001 From: shafiekwalker7861 Date: Tue, 28 Jul 2026 09:44:20 +0200 Subject: [PATCH 2/3] Complete Sprint 2 interpret exercise --- Sprint-2/interpret/invert.js | 28 +++++++++++++--------------- Sprint-2/interpret/invert.test.js | 12 ++++++++++++ 2 files changed, 25 insertions(+), 15 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 bb353fb1f..91efd58bf 100644 --- a/Sprint-2/interpret/invert.js +++ b/Sprint-2/interpret/invert.js @@ -1,29 +1,27 @@ -// 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; + invertedObj[value] = key; } return invertedObj; } -// a) What is the current return value when invert is called with { a : 1 } +// a) Before the fix, invert({ a: 1 }) returned: +// { key: 1 } -// b) What is the current return value when invert is called with { a: 1, b: 2 } +// b) Before the fix, invert({ a: 1, b: 2 }) returned: +// { key: 2 } +// The second loop replaced the first value. -// c) What is the target return value when invert is called with {a : 1, b: 2} +// c) The target return value is: +// { "1": "a", "2": "b" } -// c) What does Object.entries return? Why is it needed in this program? +// d) Object.entries returns an array of [key, value] pairs. +// It allows the loop to access both parts of each property. -// d) Explain why the current return value is different from the target output +// e) The original code used dot notation, which created a property +// literally named "key" instead of using the value dynamically. -// 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/interpret/invert.test.js b/Sprint-2/interpret/invert.test.js new file mode 100644 index 000000000..d4e491707 --- /dev/null +++ b/Sprint-2/interpret/invert.test.js @@ -0,0 +1,12 @@ +const invert = require("./invert.js"); + +test("swaps the keys and values in an object", () => { + expect(invert({ a: 1, b: 2 })).toEqual({ + 1: "a", + 2: "b", + }); +}); + +test("returns an empty object for an empty object", () => { + expect(invert({})).toEqual({}); +}); \ No newline at end of file From d6163d6f25c7819c700526654050fdd523e0a5d3 Mon Sep 17 00:00:00 2001 From: shafiekwalker7861 Date: Tue, 28 Jul 2026 09:51:49 +0200 Subject: [PATCH 3/3] Complete Sprint 2 stretch exercise --- Sprint-2/stretch/count-words.js | 20 ++++++++++++++++ Sprint-2/stretch/count-words.test.js | 22 +++++++++++++++++ Sprint-2/stretch/mode.js | 36 +++++++++++++++++----------- Sprint-2/stretch/till.js | 25 +++++++------------ Sprint-2/stretch/till.test.js | 16 +++++++++++++ 5 files changed, 88 insertions(+), 31 deletions(-) create mode 100644 Sprint-2/stretch/count-words.test.js create mode 100644 Sprint-2/stretch/till.test.js diff --git a/Sprint-2/stretch/count-words.js b/Sprint-2/stretch/count-words.js index 8e85d19d7..42b4bcb85 100644 --- a/Sprint-2/stretch/count-words.js +++ b/Sprint-2/stretch/count-words.js @@ -26,3 +26,23 @@ 3. Order the results to find out which word is the most common in the input */ + +function countWords(text) { + const cleanedText = text + .toLowerCase() + .replace(/[.,!?]/g, ""); + + const words = cleanedText + .split(" ") + .filter((word) => word !== ""); + + const wordCounts = {}; + + for (const word of words) { + wordCounts[word] = (wordCounts[word] || 0) + 1; + } + + return wordCounts; +} + +module.exports = countWords; diff --git a/Sprint-2/stretch/count-words.test.js b/Sprint-2/stretch/count-words.test.js new file mode 100644 index 000000000..0dde28e4d --- /dev/null +++ b/Sprint-2/stretch/count-words.test.js @@ -0,0 +1,22 @@ +const countWords = require("./count-words.js"); + +test("counts how many times each word appears", () => { + expect(countWords("you and me and you")).toEqual({ + you: 2, + and: 2, + me: 1, + }); +}); + +test("ignores capital letters and punctuation", () => { + expect(countWords("Hello, hello! How are you?")).toEqual({ + hello: 2, + how: 1, + are: 1, + you: 1, + }); +}); + +test("returns an empty object for an empty string", () => { + expect(countWords("")).toEqual({}); +}); \ No newline at end of file diff --git a/Sprint-2/stretch/mode.js b/Sprint-2/stretch/mode.js index 3f7609d79..892abbddf 100644 --- a/Sprint-2/stretch/mode.js +++ b/Sprint-2/stretch/mode.js @@ -8,29 +8,37 @@ // 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(); +function countFrequencies(list) { + const frequencies = new Map(); - for (let num of list) { - if (typeof num !== "number") { + for (const number of list) { + if (typeof number !== "number") { continue; } - freqs.set(num, (freqs.get(num) || 0) + 1); + frequencies.set(number, (frequencies.get(number) || 0) + 1); } - // Find the value with the highest frequency - let maxFreq = 0; + return frequencies; +} + +function findMostFrequent(frequencies) { + let highestFrequency = 0; let mode; - for (let [num, freq] of freqs) { - if (freq > maxFreq) { - mode = num; - maxFreq = freq; + + for (const [number, frequency] of frequencies) { + if (frequency > highestFrequency) { + highestFrequency = frequency; + mode = number; } } - return maxFreq === 0 ? NaN : mode; + return highestFrequency === 0 ? NaN : mode; +} + +function calculateMode(list) { + const frequencies = countFrequencies(list); + return findMostFrequent(frequencies); } -module.exports = calculateMode; +module.exports = calculateMode; \ No newline at end of file diff --git a/Sprint-2/stretch/till.js b/Sprint-2/stretch/till.js index 6a08532e7..df56ac9cd 100644 --- a/Sprint-2/stretch/till.js +++ b/Sprint-2/stretch/till.js @@ -1,31 +1,22 @@ // totalTill takes an object representing coins in a till -// Given an object of coins -// When this till object is passed to totalTill -// Then it should return the total amount in pounds - function totalTill(till) { let total = 0; for (const [coin, quantity] of Object.entries(till)) { - total += coin * quantity; + const coinValue = parseInt(coin, 10); + total += coinValue * quantity; } return `£${total / 100}`; } -const till = { - "1p": 10, - "5p": 6, - "50p": 4, - "20p": 10, -}; -const totalAmount = totalTill(till); - -// a) What is the target output when totalTill is called with the till object +// a) The target output is £4.4 -// b) Why do we need to use Object.entries inside the for...of loop in this function? +// b) Object.entries converts the object into key-value pairs, +// allowing us to access both the coin and its quantity. -// c) What does coin * quantity evaluate to inside the for...of loop? +// c) coinValue * quantity calculates the total value +// of that type of coin. -// d) Write a test for this function to check it works and then fix the implementation of totalTill +module.exports = totalTill; \ No newline at end of file diff --git a/Sprint-2/stretch/till.test.js b/Sprint-2/stretch/till.test.js new file mode 100644 index 000000000..5444d7bfa --- /dev/null +++ b/Sprint-2/stretch/till.test.js @@ -0,0 +1,16 @@ +const totalTill = require("./till.js"); + +test("calculates the total amount in the till", () => { + const till = { + "1p": 10, + "5p": 6, + "50p": 4, + "20p": 10, + }; + + expect(totalTill(till)).toBe("£4.4"); +}); + +test("returns £0 for an empty till", () => { + expect(totalTill({})).toBe("£0"); +}); \ No newline at end of file