From cf0da2d3c5a3c6960d24564d74513c720e725895 Mon Sep 17 00:00:00 2001 From: Daniel Aderibigbe Date: Sat, 14 Mar 2026 11:45:31 +0000 Subject: [PATCH 1/3] debug task completed --- Sprint-2/debug/address.js | 4 +++- Sprint-2/debug/author.js | 6 ++++-- Sprint-2/debug/recipe.js | 10 +++++++--- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/Sprint-2/debug/address.js b/Sprint-2/debug/address.js index 940a6af83..628fc854c 100644 --- a/Sprint-2/debug/address.js +++ b/Sprint-2/debug/address.js @@ -1,5 +1,7 @@ // Predict and explain first... +// The code is trying to log the house number from the address object, but it is using the wrong syntax to access the property. The code is currently using address[0], which is incorrect because it is trying to access the first element of an array, but address is an object, not an array. + // This code should log out the houseNumber from the address object // but it isn't working... // Fix anything that 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..abb6e39fe 100644 --- a/Sprint-2/debug/author.js +++ b/Sprint-2/debug/author.js @@ -1,5 +1,7 @@ // Predict and explain first... +// This program attempt to log out all the property values in the object, It isnt working because we are trying to iterate over an object using a for... of loop, which is not valid. To fix this, we can use a for... in loop to iterate over the keys of the object and then access the corresponding values. + // 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) { - console.log(value); +for (const key in author) { + console.log(author[key]); } diff --git a/Sprint-2/debug/recipe.js b/Sprint-2/debug/recipe.js index 6cbdd22cd..157db3bee 100644 --- a/Sprint-2/debug/recipe.js +++ b/Sprint-2/debug/recipe.js @@ -1,5 +1,7 @@ // Predict and explain first... +// The code is trying to log out the title, how many it serves and the ingredients of a recipe. However, it is not working because when we try to log the recipe object directly, it will not format the output as intended. Instead, we should access each property of the recipe object separately and format the output accordingly. + // 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? @@ -10,6 +12,8 @@ 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 ff8cfe0b829864df4685a8df53d5334ae1c3f0c5 Mon Sep 17 00:00:00 2001 From: Daniel Aderibigbe Date: Thu, 19 Mar 2026 10:55:19 +0000 Subject: [PATCH 2/3] test update --- Sprint-2/implement/contains.js | 4 +++- Sprint-2/implement/contains.test.js | 34 ++++++++++++++++++++++------- Sprint-2/implement/lookup.js | 8 +++++-- 3 files changed, 35 insertions(+), 11 deletions(-) diff --git a/Sprint-2/implement/contains.js b/Sprint-2/implement/contains.js index cd779308a..0555510b7 100644 --- a/Sprint-2/implement/contains.js +++ b/Sprint-2/implement/contains.js @@ -1,3 +1,5 @@ -function contains() {} +function contains(object, property) { + return Object.prototype.hasOwnProperty.call(object, property); +} module.exports = contains; diff --git a/Sprint-2/implement/contains.test.js b/Sprint-2/implement/contains.test.js index 326bdb1f2..3a7efbd25 100644 --- a/Sprint-2/implement/contains.test.js +++ b/Sprint-2/implement/contains.test.js @@ -1,35 +1,53 @@ const contains = require("./contains.js"); /* -Implement a function called contains that checks an object contains a -particular property + 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}, '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' -*/ + 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 +test("contains checks if an object contains a particular property", () => { + const obj = { a: 1, b: 2 }; + expect(contains(obj, "a")).toBe(true); + expect(contains(obj, "c")).toBe(false); +}); // 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("contains an object with existing property returns true", () => { + 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("contains an object with non-existent property returns false", () => { + 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 +test("contains with invalid parameters returns false", () => { + expect(contains([], "a")).toBe(false); +}); diff --git a/Sprint-2/implement/lookup.js b/Sprint-2/implement/lookup.js index a6746e07f..bf663ca33 100644 --- a/Sprint-2/implement/lookup.js +++ b/Sprint-2/implement/lookup.js @@ -1,5 +1,9 @@ -function createLookup() { - // implementation here +function createLookup(countryCurrency) { + const lookup = {}; + for (const [country, currency] of countryCurrency) { + lookup[country] = currency; + } + return lookup; } module.exports = createLookup; From 6750bf50f6fab9a71ba6789347b248f4e44ba39c Mon Sep 17 00:00:00 2001 From: Daniel Aderibigbe Date: Fri, 20 Mar 2026 11:37:30 +0000 Subject: [PATCH 3/3] stretch exercises completed --- Sprint-2/implement/lookup.test.js | 16 ++++++++++- Sprint-2/implement/querystring.js | 13 +++++++-- Sprint-2/implement/querystring.test.js | 27 ++++++++++++++++-- Sprint-2/implement/tally.js | 18 +++++++++++- Sprint-2/implement/tally.test.js | 14 +++++++++- Sprint-2/interpret/invert.js | 20 +++++++++++++- Sprint-2/interpret/invert.test.js | 18 ++++++++++++ Sprint-2/stretch/count-words.js | 21 ++++++++++++++ Sprint-2/stretch/count-words.test.js | 13 +++++++++ Sprint-2/stretch/mode.js | 20 ++++++++++---- Sprint-2/stretch/till.js | 38 +++++++++++++++++++++++++- Sprint-2/stretch/till.test.js | 12 ++++++++ 12 files changed, 215 insertions(+), 15 deletions(-) create mode 100644 Sprint-2/interpret/invert.test.js create mode 100644 Sprint-2/stretch/count-words.test.js create mode 100644 Sprint-2/stretch/till.test.js diff --git a/Sprint-2/implement/lookup.test.js b/Sprint-2/implement/lookup.test.js index 547e06c5a..288a21eaf 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 input = [ + ["US", "USD"], + ["CA", "CAD"], + ]; + + const expectedOutput = { + US: "USD", + CA: "CAD", + }; + + const result = createLookup(input); + + expect(result).toEqual(expectedOutput); +}); /* diff --git a/Sprint-2/implement/querystring.js b/Sprint-2/implement/querystring.js index 45ec4e5f3..953f12cc2 100644 --- a/Sprint-2/implement/querystring.js +++ b/Sprint-2/implement/querystring.js @@ -1,13 +1,22 @@ 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; + const indexOfEquals = pair.indexOf("="); + + if (indexOfEquals === -1) { + queryParams[pair] = ""; + } else { + const key = pair.slice(0, indexOfEquals); + const value = pair.slice(indexOfEquals + 1); + queryParams[key] = value; + } } return queryParams; diff --git a/Sprint-2/implement/querystring.test.js b/Sprint-2/implement/querystring.test.js index 3e218b789..1ed22913f 100644 --- a/Sprint-2/implement/querystring.test.js +++ b/Sprint-2/implement/querystring.test.js @@ -3,10 +3,33 @@ // Below is one test case for an edge case the implementation doesn't handle well. // Fix the implementation for this test, and try to think of as many other edge cases as possible - write tests and fix those too. -const parseQueryString = require("./querystring.js") +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 an empty object for an empty query string", () => { + expect(parseQueryString("")).toEqual({}); +}); + +test("parses multiple key value pairs", () => { + expect(parseQueryString("name=dan&age=20")).toEqual({ + name: "dan", + age: "20", + }); +}); + +test("handles a key with an empty value", () => { + expect(parseQueryString("name=")).toEqual({ + name: "", + }); +}); + +test("handles a key with no equals sign", () => { + expect(parseQueryString("name")).toEqual({ + name: "", }); }); diff --git a/Sprint-2/implement/tally.js b/Sprint-2/implement/tally.js index f47321812..60e1939f4 100644 --- a/Sprint-2/implement/tally.js +++ b/Sprint-2/implement/tally.js @@ -1,3 +1,19 @@ -function tally() {} +function tally(items) { + if (!Array.isArray(items)) { + throw new Error("Invalid input"); + } + + const result = {}; + + for (const item of items) { + if (result[item]) { + result[item]++; + } else { + result[item] = 1; + } + } + + return result; +} module.exports = tally; diff --git a/Sprint-2/implement/tally.test.js b/Sprint-2/implement/tally.test.js index 2ceffa8dd..9572e8065 100644 --- a/Sprint-2/implement/tally.test.js +++ b/Sprint-2/implement/tally.test.js @@ -23,12 +23,24 @@ 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({}); +}); // Given an array with duplicate items // When passed to tally // Then it should return counts for each unique item +test("tally counts duplicate items correctly", () => { + 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("tally throws an error for invalid input", () => { + expect(() => tally("not an array")).toThrow(); +}); diff --git a/Sprint-2/interpret/invert.js b/Sprint-2/interpret/invert.js index bb353fb1f..450aac333 100644 --- a/Sprint-2/interpret/invert.js +++ b/Sprint-2/interpret/invert.js @@ -10,20 +10,38 @@ function invert(obj) { const invertedObj = {}; for (const [key, value] of Object.entries(obj)) { - invertedObj.key = value; + invertedObj[value] = key; } return invertedObj; } +module.exports = invert; + // 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 from the object. +// Object.entries({ a: 1, b: 2 }); +// returns: [["a", 1], ["b", 2]] +//It is needed so we can loop through both the keys and values of the object at the same time. + // d) Explain why the current return value is different from the target output +//The current implementation is incorrect because it uses: +// invertedObj.key = value; +// This creates a property literally called "key" instead of using the actual key/value dynamically. +//So it overwrites the same property each time instead of building the correct object. + // e) 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..acb02a1c9 --- /dev/null +++ b/Sprint-2/interpret/invert.test.js @@ -0,0 +1,18 @@ +const invert = require("./invert.js"); + +test("inverts a single key-value pair", () => { + expect(invert({ a: 1 })).toEqual({ + 1: "a", + }); +}); + +test("inverts multiple key-value pairs", () => { + expect(invert({ a: 1, b: 2 })).toEqual({ + 1: "a", + 2: "b", + }); +}); + +test("returns empty object when given empty object", () => { + expect(invert({})).toEqual({}); +}); diff --git a/Sprint-2/stretch/count-words.js b/Sprint-2/stretch/count-words.js index 8e85d19d7..cc048f206 100644 --- a/Sprint-2/stretch/count-words.js +++ b/Sprint-2/stretch/count-words.js @@ -26,3 +26,24 @@ 3. Order the results to find out which word is the most common in the input */ +function countWords(str) { + const result = {}; + + if (str.length === 0) { + return result; + } + + const words = str.split(" "); + + for (const word of words) { + if (result[word]) { + result[word]++; + } else { + result[word] = 1; + } + } + + return result; +} + +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..57f2a9b0a --- /dev/null +++ b/Sprint-2/stretch/count-words.test.js @@ -0,0 +1,13 @@ +const countWords = require("./count-words.js"); + +test("counts words correctly", () => { + expect(countWords("you and me and you")).toEqual({ + you: 2, + and: 2, + me: 1, + }); +}); + +test("returns empty object for empty string", () => { + expect(countWords("")).toEqual({}); +}); diff --git a/Sprint-2/stretch/mode.js b/Sprint-2/stretch/mode.js index 3f7609d79..26062c07e 100644 --- a/Sprint-2/stretch/mode.js +++ b/Sprint-2/stretch/mode.js @@ -8,11 +8,10 @@ // 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 getFrequencies(list) { + const freqs = new Map(); - for (let num of list) { + for (const num of list) { if (typeof num !== "number") { continue; } @@ -20,10 +19,14 @@ function calculateMode(list) { freqs.set(num, (freqs.get(num) || 0) + 1); } - // Find the value with the highest frequency + return freqs; +} + +function getMode(freqs) { let maxFreq = 0; let mode; - for (let [num, freq] of freqs) { + + for (const [num, freq] of freqs) { if (freq > maxFreq) { mode = num; maxFreq = freq; @@ -33,4 +36,9 @@ function calculateMode(list) { return maxFreq === 0 ? NaN : mode; } +function calculateMode(list) { + const freqs = getFrequencies(list); + return getMode(freqs); +} + module.exports = calculateMode; diff --git a/Sprint-2/stretch/till.js b/Sprint-2/stretch/till.js index 6a08532e7..82a89f0c2 100644 --- a/Sprint-2/stretch/till.js +++ b/Sprint-2/stretch/till.js @@ -4,7 +4,7 @@ // When this till object is passed to totalTill // Then it should return the total amount in pounds -function totalTill(till) { +/*function totalTill(till) { let total = 0; for (const [coin, quantity] of Object.entries(till)) { @@ -21,11 +21,47 @@ const till = { "20p": 10, }; 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 use Object.entries(till) so we can loop through both the coin name and the quantity at the same time. +//Example: Object.entries(till); +// returns: +/*[ + ["1p", 10], + ["5p", 6], + ["50p", 4], + ["20p", 10] +] +*/ +//This is needed because for...of works with arrays/iterables, and Object.entries turns the object into an array of key-value pairs that we can loop through. + // c) What does coin * quantity evaluate to inside the for...of loop? +// Right now coin is a string like "1p" or "50p". + +//So inside the loop: coin * quantity, becomes things like: +//"1p" * 10 , "50p" * 4 + +//These evaluate to NaN because strings like "1p" and "50p" are not pure numbers. +//That’s why the implementation is broken. + // d) Write a test for this function to check it works and then fix the implementation of totalTill + +function totalTill(till) { + let total = 0; + + for (const [coin, quantity] of Object.entries(till)) { + const coinValue = Number(coin.replace("p", "")); + total += coinValue * quantity; + } + + return `£${(total / 100).toFixed(2)}`; +} + +module.exports = totalTill; diff --git a/Sprint-2/stretch/till.test.js b/Sprint-2/stretch/till.test.js new file mode 100644 index 000000000..a7dda2290 --- /dev/null +++ b/Sprint-2/stretch/till.test.js @@ -0,0 +1,12 @@ +const totalTill = require("./till.js"); + +test("returns the total amount in pounds for a till object", () => { + const till = { + "1p": 10, + "5p": 6, + "50p": 4, + "20p": 10, + }; + + expect(totalTill(till)).toEqual("£4.40"); +});