From 46f56120f038b89ef4d840610295c84f47b650f4 Mon Sep 17 00:00:00 2001 From: Roger Lee Date: Tue, 10 Nov 2026 09:24:22 +0000 Subject: [PATCH 1/9] Solved the bug of address. --- Sprint-2/debug/address.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Sprint-2/debug/address.js b/Sprint-2/debug/address.js index 940a6af832..a4f2aa0de2 100644 --- a/Sprint-2/debug/address.js +++ b/Sprint-2/debug/address.js @@ -1,5 +1,8 @@ // Predict and explain first... +//The function intent to return houseNumber. However, console.log should locate the key +//instead of index. function will return error before fix. + // This code should log out the houseNumber from the address object // but it isn't working... // Fix anything that isn't working @@ -12,4 +15,4 @@ const address = { postcode: "XYZ 123", }; -console.log(`My house number is ${address[0]}`); +console.log(`My house number is ${address["houseNumber"]}`); From 323e17f31e74c58296380a6e254721e6de507668 Mon Sep 17 00:00:00 2001 From: Roger Lee Date: Tue, 10 Nov 2026 09:39:09 +0000 Subject: [PATCH 2/9] fix problem of author --- Sprint-2/debug/author.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Sprint-2/debug/author.js b/Sprint-2/debug/author.js index 8c2125977d..f6aff82c5b 100644 --- a/Sprint-2/debug/author.js +++ b/Sprint-2/debug/author.js @@ -1,5 +1,7 @@ // Predict and explain first... +//The function should return for all values in the object. But not sure the return before fix the problem + // 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,4 @@ const author = { alive: true, }; -for (const value of author) { - console.log(value); -} +console.log(Object.values(author)); From 851a28d2be6548d5afa3960eba703527f5d71424 Mon Sep 17 00:00:00 2001 From: Roger Lee Date: Tue, 10 Nov 2026 09:52:54 +0000 Subject: [PATCH 3/9] fix the problem of recipe --- Sprint-2/debug/recipe.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Sprint-2/debug/recipe.js b/Sprint-2/debug/recipe.js index 6cbdd22cdc..76d8aff121 100644 --- a/Sprint-2/debug/recipe.js +++ b/Sprint-2/debug/recipe.js @@ -1,4 +1,5 @@ // Predict and explain first... +// Ingredients will return the whole recipe before fix. // This program should log out the title, how many it serves and the ingredients. // Each ingredient should be logged on a new line @@ -12,4 +13,4 @@ const recipe = { console.log(`${recipe.title} serves ${recipe.serves} ingredients: -${recipe}`); +${recipe.ingredients.join("\n")}`); From ed6358c1697ea7abbeba126905dea2938677b950 Mon Sep 17 00:00:00 2001 From: Roger Lee Date: Tue, 10 Nov 2026 16:37:38 +0000 Subject: [PATCH 4/9] completed test and function of contains. --- Sprint-2/implement/contains.js | 11 ++++- Sprint-2/implement/contains.test.js | 66 +++++++++++++++++++++-------- 2 files changed, 58 insertions(+), 19 deletions(-) diff --git a/Sprint-2/implement/contains.js b/Sprint-2/implement/contains.js index cd779308a0..8c5e8531a7 100644 --- a/Sprint-2/implement/contains.js +++ b/Sprint-2/implement/contains.js @@ -1,3 +1,12 @@ -function contains() {} +function contains(testObject, keyword) { + if ( + typeof testObject === "object" && + testObject !== null && + Array.isArray(testObject) === false + ) { + return Object.hasOwn(testObject, keyword); + } + throw new Error("contains requires an object"); +} module.exports = contains; diff --git a/Sprint-2/implement/contains.test.js b/Sprint-2/implement/contains.test.js index 719272787d..a06596f686 100644 --- a/Sprint-2/implement/contains.test.js +++ b/Sprint-2/implement/contains.test.js @@ -20,21 +20,51 @@ as an array isn't an object // When passed an object and a property name // Then it should return true if the object contains the property, false otherwise -// Given an empty object -// When passed to contains -// Then it should return false -test.todo("contains on empty object returns false"); - -// Given an object with properties -// When passed to contains with an existing property name -// Then it should return true - -// Given an object with properties -// When passed to contains with a non-existent property name -// Then it should return 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") +describe("contains", () => { + // Given an empty object + // When passed to contains + // Then it should return false + + test("return for {}", () => { + expect(contains({})).toBe(false); + }); + + // Given an object with properties + // When passed to contains with an existing property name + // Then it should return true + test("return for {a: 1, b: 2}, 'a'", () => { + 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("return for {a: 1, b: 2}, 'c'", () => { + expect(contains({ a: 1, b: 2 }, "c")).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("return for ['a', 'b', 'c']", () => { + expect(() => contains(["a", "b", "c"])).toThrow( + "contains requires an object" + ); + }); + + test("return for null", () => { + expect(() => contains(null)).toThrow("contains requires an object"); + }); + + test("return for 'It is a string.'", () => { + expect(() => contains("It is a string.")).toThrow( + "contains requires an object" + ); + }); + + test("return for 10", () => { + expect(() => contains(10)).toThrow("contains requires an object"); + }); +}); From 5bd55e45f273dfa86be114836affe6ba12d53cfc Mon Sep 17 00:00:00 2001 From: Roger Lee Date: Tue, 10 Nov 2026 17:25:20 +0000 Subject: [PATCH 5/9] completed test and function of lookup --- Sprint-2/implement/lookup.js | 3 ++- Sprint-2/implement/lookup.test.js | 9 ++++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/Sprint-2/implement/lookup.js b/Sprint-2/implement/lookup.js index a6746e07f5..74caf42148 100644 --- a/Sprint-2/implement/lookup.js +++ b/Sprint-2/implement/lookup.js @@ -1,5 +1,6 @@ -function createLookup() { +function createLookup(lookUpPairs) { // implementation here + return Object.fromEntries(lookUpPairs); } module.exports = createLookup; diff --git a/Sprint-2/implement/lookup.test.js b/Sprint-2/implement/lookup.test.js index 547e06c5a7..286f4ef8e4 100644 --- a/Sprint-2/implement/lookup.test.js +++ b/Sprint-2/implement/lookup.test.js @@ -1,6 +1,13 @@ const createLookup = require("./lookup.js"); -test.todo("creates a country currency code lookup for multiple codes"); +describe("createLookup", () => { + test("return a object for ([['US', 'USD'], ['CA', 'CAD']])", () => { + expect(createLookup([["US", "USD"], ["CA", "CAD"]])).toEqual({ + US: "USD", + CA: "CAD", + })}) +}); + /* From 1f1e59f658ecb21d04bcd3dfc8027427190b2556 Mon Sep 17 00:00:00 2001 From: Roger Lee Date: Tue, 10 Nov 2026 17:25:52 +0000 Subject: [PATCH 6/9] formatted document for lookup.test.js --- Sprint-2/implement/lookup.test.js | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/Sprint-2/implement/lookup.test.js b/Sprint-2/implement/lookup.test.js index 286f4ef8e4..4091235f55 100644 --- a/Sprint-2/implement/lookup.test.js +++ b/Sprint-2/implement/lookup.test.js @@ -2,13 +2,18 @@ const createLookup = require("./lookup.js"); describe("createLookup", () => { test("return a object for ([['US', 'USD'], ['CA', 'CAD']])", () => { - expect(createLookup([["US", "USD"], ["CA", "CAD"]])).toEqual({ - US: "USD", - CA: "CAD", - })}) + expect( + createLookup([ + ["US", "USD"], + ["CA", "CAD"], + ]) + ).toEqual({ + US: "USD", + CA: "CAD", + }); + }); }); - /* Create a lookup object of key value pairs from an array of code pairs From 78485fbc2278e6858ecf1eac584e273ffb8151c0 Mon Sep 17 00:00:00 2001 From: Roger Lee Date: Thu, 12 Nov 2026 17:23:35 +0000 Subject: [PATCH 7/9] completed test and function of querystring.js --- Sprint-2/implement/querystring.js | 26 +++++++++++++++++++++++++- Sprint-2/implement/querystring.test.js | 8 -------- 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/Sprint-2/implement/querystring.js b/Sprint-2/implement/querystring.js index 45ec4e5f37..190a70ce38 100644 --- a/Sprint-2/implement/querystring.js +++ b/Sprint-2/implement/querystring.js @@ -3,14 +3,38 @@ function parseQueryString(queryString) { if (queryString.length === 0) { return queryParams; } + const keyValuePairs = queryString.split("&"); for (const pair of keyValuePairs) { - const [key, value] = pair.split("="); + if (!pair.includes("=") && pair.length > 0) { + queryParams[pair] = ""; + continue; + } + + const key = decodeURIComponent(pair.slice(0, pair.indexOf("="))).replace( + "+", + " " + ); + const value = decodeURIComponent(pair.slice(pair.indexOf("=") + 1)).replace( + "+", + " " + ); + + if (pair.indexOf("=") !== -1) { + queryParams[key] = value; + } + + if (key.length === 0 && value.length === 0) { + continue; + } + queryParams[key] = value; } return queryParams; } +console.log(parseQueryString("full+name=John+Doe")); + module.exports = parseQueryString; diff --git a/Sprint-2/implement/querystring.test.js b/Sprint-2/implement/querystring.test.js index 328b8df617..6f70185c6c 100644 --- a/Sprint-2/implement/querystring.test.js +++ b/Sprint-2/implement/querystring.test.js @@ -37,12 +37,4 @@ test("should replace '+' by ' '", () => { }); }); -// 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 6f72c2a3fae9587f9958b42f039f8fcf47d617b8 Mon Sep 17 00:00:00 2001 From: Roger Lee Date: Thu, 12 Nov 2026 20:37:44 +0000 Subject: [PATCH 8/9] completed test and function of tally. --- Sprint-2/implement/tally.js | 16 +++++++++++++++- Sprint-2/implement/tally.test.js | 18 +++++++++++++++++- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/Sprint-2/implement/tally.js b/Sprint-2/implement/tally.js index f47321812e..4f4f1115f2 100644 --- a/Sprint-2/implement/tally.js +++ b/Sprint-2/implement/tally.js @@ -1,3 +1,17 @@ -function tally() {} +function tally(items) { + if (!Array.isArray(items)) { + throw new Error("tally requires an array"); + } + const counter = {}; + for (const item of items) { + if (Object.hasOwn(counter, item) === false) { + counter[item] = 1; + } else { + counter[item]++; + } + } + + return counter; +} module.exports = tally; diff --git a/Sprint-2/implement/tally.test.js b/Sprint-2/implement/tally.test.js index ca763c2965..d78c7967cf 100644 --- a/Sprint-2/implement/tally.test.js +++ b/Sprint-2/implement/tally.test.js @@ -23,12 +23,28 @@ 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("return empty object for []", () => { + expect(tally([])).toEqual({}); +}); // Given an array with duplicate items // When passed to tally // Then it should return counts for each unique item +test("return a list of items and count the frequency of each item", () => { + expect(tally(["a", "a", "a", "b", "b"])).toEqual({ a: 3, b: 2 }); +}); // 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("throw error for 'It is a string'", () => { + expect(() => tally().toThrow("tally requires an array")); +}); + +test("throw error for 10", () => { + expect(() => tally(10).toThrow("tally requires an array")); +}); + +test("throw error for ''", () => { + expect(() => tally().toThrow("tally requires an array")); +}); From 221cf1adf2aa6b21b3a020c0416421a497cc62e6 Mon Sep 17 00:00:00 2001 From: Roger Lee Date: Sat, 14 Nov 2026 15:21:32 +0000 Subject: [PATCH 9/9] complete the test and function of invert --- Sprint-2/interpret/invert.js | 16 +++++++++++++++- Sprint-2/interpret/invert.test.js | 21 +++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 Sprint-2/interpret/invert.test.js diff --git a/Sprint-2/interpret/invert.js b/Sprint-2/interpret/invert.js index bb353fb1fd..4503b3afda 100644 --- a/Sprint-2/interpret/invert.js +++ b/Sprint-2/interpret/invert.js @@ -10,20 +10,34 @@ function invert(obj) { const invertedObj = {}; for (const [key, value] of Object.entries(obj)) { - invertedObj.key = value; + if (Object.hasOwn(invertedObj, value)) { + throw new Error(`Duplicate key found after inversion: "${value}"`); + } + invertedObj[value] = key; } return invertedObj; } + + + // 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? +// [ [ 'a', 1 ], [ 'b', 2 ] ]. Object.entries convert object to array. Then allow to use +// array's method to achieve teh goal of function. // d) Explain why the current return value is different from the target output +// invertedObj.key is not mean convert value to key. It just simply print string "key" // e) Fix the implementation of invert (and write tests to prove it's fixed!) + +module.exports = invert; diff --git a/Sprint-2/interpret/invert.test.js b/Sprint-2/interpret/invert.test.js new file mode 100644 index 0000000000..a33ca9589f --- /dev/null +++ b/Sprint-2/interpret/invert.test.js @@ -0,0 +1,21 @@ +const invert = require("./invert.js"); + +describe("invert", () => { + test("inverting key and value of object", () => { + expect(invert({ a: 1, b: 2, c: 3 })).toEqual({ 1: "a", 2: "b", 3: "c" }); + }); + + test("inverting empty object", () => { + expect(invert({})).toEqual({}); + }); + + test("inverting object with either key or value is empty", () => { + expect(invert({ "": 1, b: "" })).toEqual({ 1: "", "": "b" }); + }); + + test("inverting object with duplicate value", () => { + expect(() => invert({ a: 1, b: 1 })).toThrow( + 'Duplicate key found after inversion: "1"' + ); + }); +});