From aaf5f1162c25a6cf1f5d068be67fbd47b41248b6 Mon Sep 17 00:00:00 2001 From: abmhts Date: Wed, 11 Nov 2026 13:03:59 +0000 Subject: [PATCH 1/9] Fix - address file bug --- Sprint-2/debug/address.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Sprint-2/debug/address.js b/Sprint-2/debug/address.js index 940a6af832..bc5e21fa6d 100644 --- a/Sprint-2/debug/address.js +++ b/Sprint-2/debug/address.js @@ -1,5 +1,5 @@ // Predict and explain first... - +//To access object properties using their keys, not array indexes. // 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}`); From e6e87b7b1bba317f46d7dca1ab0e7b55a1593943 Mon Sep 17 00:00:00 2001 From: abmhts Date: Wed, 11 Nov 2026 13:43:50 +0000 Subject: [PATCH 2/9] Fix - author file bug --- Sprint-2/debug/author.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Sprint-2/debug/author.js b/Sprint-2/debug/author.js index 8c2125977d..04d44dac31 100644 --- a/Sprint-2/debug/author.js +++ b/Sprint-2/debug/author.js @@ -1,5 +1,5 @@ // Predict and explain first... - +//we need to add Object.values(author) to returns an array containing all the values of the object. // 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,8 @@ const author = { alive: true, }; -for (const value of author) { +// for (const value of author) { +// console.log(value); +for (const value of Object.values(author)) { console.log(value); } From 07df80868c2116507757c1e0c5937969fdf62cbd Mon Sep 17 00:00:00 2001 From: abmhts Date: Wed, 11 Nov 2026 14:38:21 +0000 Subject: [PATCH 3/9] Fix - recipe file bug --- Sprint-2/debug/recipe.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/Sprint-2/debug/recipe.js b/Sprint-2/debug/recipe.js index 6cbdd22cdc..303c61d526 100644 --- a/Sprint-2/debug/recipe.js +++ b/Sprint-2/debug/recipe.js @@ -1,5 +1,5 @@ // Predict and explain first... - +//The title and number of servers will display correctly. However, since the ingredients are stored in an array, I can use recipe.ingredients with a for...of loop to iterate through the array and display each ingredient on a new line. // 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 +10,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} + ingredients:`); +for (const ingredient of recipe.ingredients) { + console.log(ingredient); +} From d86885d20bf447847b2b07dc69ba4f5878c13189 Mon Sep 17 00:00:00 2001 From: abmhts Date: Thu, 12 Nov 2026 11:02:01 +0000 Subject: [PATCH 4/9] Implement a function - contains --- Sprint-2/implement/contains.js | 7 ++++++- Sprint-2/implement/contains.test.js | 18 ++++++++++++++++-- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/Sprint-2/implement/contains.js b/Sprint-2/implement/contains.js index cd779308a0..61ef89a6ec 100644 --- a/Sprint-2/implement/contains.js +++ b/Sprint-2/implement/contains.js @@ -1,3 +1,8 @@ -function contains() {} +function contains(object,property) { + if (typeof object !== "object" || object === null || Array.isArray(object)) { + throw new Error("contains requires an object"); + } + return Object.hasOwn(object, property); +} module.exports = contains; diff --git a/Sprint-2/implement/contains.test.js b/Sprint-2/implement/contains.test.js index 719272787d..65c077eb40 100644 --- a/Sprint-2/implement/contains.test.js +++ b/Sprint-2/implement/contains.test.js @@ -23,18 +23,32 @@ as an array isn't an object // 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("given an object with properties returns true", () => { + 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("given an object with properties contains with a non-existent property name returns false", () => { + expect(contains({c: 1, b: 2}, "a")).toBe(false); +}); // Given a value that isn't an object - an array, a string, a number, // null, or no argument at all // When passed to contains // Then it should throw Error("contains requires an object") // (careful: typeof [] and typeof null are both "object") +test("given a value that isn't an object - an array, a string, a number, null, or no argument at all", () => { + expect(() => contains([1, 2, 3], 'a')).toThrow("contains requires an object"); + expect(() => contains("bcd", 'a')).toThrow("contains requires an object"); + expect(() => contains(123, 'a')).toThrow("contains requires an object"); + expect(() => contains(null, 'a')).toThrow("contains requires an object"); + expect(() => contains()).toThrow("contains requires an object"); +}); \ No newline at end of file From c29c10ba64b0079710a074f0a857171f02c0596b Mon Sep 17 00:00:00 2001 From: abmhts Date: Thu, 12 Nov 2026 11:26:40 +0000 Subject: [PATCH 5/9] Implement a function - lookup --- Sprint-2/implement/lookup.js | 13 +++++++++++-- Sprint-2/implement/lookup.test.js | 14 +++++++++++++- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/Sprint-2/implement/lookup.js b/Sprint-2/implement/lookup.js index a6746e07f5..0caaa7e9fd 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 c_Lookup = {}; + + for (let i = 0; i < countryCurrencyPairs.length; i++) { + const country = countryCurrencyPairs[i][0]; + const currency = countryCurrencyPairs[i][1]; + + c_Lookup[country] = currency; + } + + return c_Lookup; } module.exports = createLookup; diff --git a/Sprint-2/implement/lookup.test.js b/Sprint-2/implement/lookup.test.js index 547e06c5a7..fccd6cd26e 100644 --- a/Sprint-2/implement/lookup.test.js +++ b/Sprint-2/implement/lookup.test.js @@ -1,6 +1,18 @@ 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"], + ["GB", "GPB"], + ]; + + expect(createLookup(countryCurrencyPairs)).toEqual({ + US: "USD", + CA: "CAD", + GB: "GPB", + }); +}); /* From 37baeb80d6fb163c721ae462f1dfabd00b483da6 Mon Sep 17 00:00:00 2001 From: abmhts Date: Fri, 13 Nov 2026 11:01:04 +0000 Subject: [PATCH 6/9] Implement a function - querystring --- Sprint-2/implement/querystring.js | 19 ++++++++++++++- Sprint-2/implement/querystring.test.js | 32 ++++++++++++++++++++------ 2 files changed, 43 insertions(+), 8 deletions(-) diff --git a/Sprint-2/implement/querystring.js b/Sprint-2/implement/querystring.js index 45ec4e5f37..8b3f590e56 100644 --- a/Sprint-2/implement/querystring.js +++ b/Sprint-2/implement/querystring.js @@ -6,7 +6,24 @@ function parseQueryString(queryString) { const keyValuePairs = queryString.split("&"); for (const pair of keyValuePairs) { - const [key, value] = pair.split("="); + if (pair === "") { + continue; + } + const equalsIndex = pair.indexOf("="); + let key; + let value; + + if (equalsIndex === -1) { + key = pair; + value = ""; + } else { + key = pair.slice(0, equalsIndex); + value = pair.slice(equalsIndex + 1); + } + + key = decodeURIComponent(key.replace(/\+/g, " ")); + value = decodeURIComponent(value.replace(/\+/g, " ")); + queryParams[key] = value; } diff --git a/Sprint-2/implement/querystring.test.js b/Sprint-2/implement/querystring.test.js index 328b8df617..d0aebc10fd 100644 --- a/Sprint-2/implement/querystring.test.js +++ b/Sprint-2/implement/querystring.test.js @@ -3,7 +3,7 @@ // Below are some test cases the implementation doesn't handle well. // Fix the implementation for these tests, 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("should parse values containing '='", () => { expect(parseQueryString("equation=a=b-2")).toEqual({ @@ -37,12 +37,30 @@ test("should replace '+' by ' '", () => { }); }); -// Stretch exercise: Handling query strings that contain identical keys +test("should return an empty object for an empty query string", () => { + expect(parseQueryString("")).toEqual({}); +}); -// 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", +test("should handle multiple equals signs", () => { + expect(parseQueryString("a=b=c=d")).toEqual({ + a: "b=c=d", + }); +}); + +test("should handle multiple values with empty values", () => { + expect(parseQueryString("name=&age=25")).toEqual({ + name: "", + age: "25", + }); +}); + +test("should decode spaces", () => { + expect(parseQueryString("first%20name=Abakar%20Souleyman")).toEqual({ + "first name": "Abakar Souleyman", + }); +}); +test("should handle multiple plus signs", () => { + expect(parseQueryString("full+user+name=A+M+S")).toEqual({ + "full user name": "A M S", }); }); From 0a4689eecb8636f664f1dddffc4d4f57a46d5bca Mon Sep 17 00:00:00 2001 From: abmhts Date: Fri, 13 Nov 2026 11:31:35 +0000 Subject: [PATCH 7/9] Implement pf function - tally --- Sprint-2/implement/tally.js | 17 ++++++++++++++++- Sprint-2/implement/tally.test.js | 22 ++++++++++++++++++---- 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/Sprint-2/implement/tally.js b/Sprint-2/implement/tally.js index f47321812e..7e84f1cf0c 100644 --- a/Sprint-2/implement/tally.js +++ b/Sprint-2/implement/tally.js @@ -1,3 +1,18 @@ -function tally() {} +function tally(items) { + if (!Array.isArray(items)) { + throw new Error("tally requires an array"); +} +const list_of_items= {}; + +for (const item of items) { + if (list_of_items [item]) { + list_of_items[item]++; + } else { + list_of_items[item] = 1; + } + +} + return list_of_items; +} module.exports = tally; diff --git a/Sprint-2/implement/tally.test.js b/Sprint-2/implement/tally.test.js index ca763c2965..443e9c4340 100644 --- a/Sprint-2/implement/tally.test.js +++ b/Sprint-2/implement/tally.test.js @@ -19,16 +19,30 @@ 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("tally passed an array of items returns an object containing the count for each unique item", () => { + expect(tally(["a"])).toEqual({a: 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("given an array with duplicate items returns counts for each unique item", () => { + expect(tally(['a', 'a', 'b', 'c'])).toEqual({a: 2, b: 1, c: 1}); +}); // Given an invalid input like a string, a number, or no argument at all // When passed to tally // Then it should throw Error("tally requires an array") + +test("throws an error for a string", () => { + expect(() => tally("abc")).toThrow("tally requires an array"); + + expect(() => tally(123)).toThrow("tally requires an array"); + + expect(() => tally()).toThrow("tally requires an array"); +}); + From e1f82e76a56e9349008d83bf8db921643cb6bd9e Mon Sep 17 00:00:00 2001 From: abmhts Date: Fri, 13 Nov 2026 12:37:36 +0000 Subject: [PATCH 8/9] interpret - invert --- Sprint-2/interpret/invert.js | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/Sprint-2/interpret/invert.js b/Sprint-2/interpret/invert.js index bb353fb1fd..3e73d4cfdc 100644 --- a/Sprint-2/interpret/invert.js +++ b/Sprint-2/interpret/invert.js @@ -10,20 +10,23 @@ function invert(obj) { const invertedObj = {}; for (const [key, value] of Object.entries(obj)) { - invertedObj.key = value; + invertedObj[value] = key; } return invertedObj; } - +console.log(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() returns an array (list) of pairs containing the keys and values from an object. +// -It's needed so we can access both the key & the value and swap them. // d) Explain why the current return value is different from the target output - +// Because invertedObj.key = value always uses the word 'key' as the property name. Instead, we should use invertedObj[value] = key to swap the original key and value. // e) Fix the implementation of invert (and write tests to prove it's fixed!) +module.exports = invert; From a7e2381f97a535edccd47f821e4d3d76a22e1790 Mon Sep 17 00:00:00 2001 From: abmhts Date: Fri, 13 Nov 2026 12:38:14 +0000 Subject: [PATCH 9/9] interpret - invert.test --- Sprint-2/interpret/invert.test.js | 8 ++++++++ 1 file changed, 8 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 0000000000..9be5beaf8f --- /dev/null +++ b/Sprint-2/interpret/invert.test.js @@ -0,0 +1,8 @@ +const invert = require("./invert.js"); + +test("should swap keys and values", () => { + expect(invert({ a: 1, b: 2 })).toEqual({ + 1: "a", + 2: "b" + }); +});