From 18c60e76f5f2a79ac53170e121f135580e6b1fe9 Mon Sep 17 00:00:00 2001 From: Khaliun Baatarkhuu Date: Wed, 22 Jul 2026 19:27:59 +0100 Subject: [PATCH 01/13] Fix address object access to retrieve house number --- Sprint-2/debug/address.js | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/Sprint-2/debug/address.js b/Sprint-2/debug/address.js index 940a6af83..59669c771 100644 --- a/Sprint-2/debug/address.js +++ b/Sprint-2/debug/address.js @@ -13,3 +13,23 @@ const address = { }; console.log(`My house number is ${address[0]}`); + +//my prediction is "undefined" because objects use names. When we js executes this code it is seeing address[0], it looks +//for 0 in objects, won't find it and will return , undefined. + +// i have run the code in devtools and got the "My house number is undefined" message. + +//the fixed code + +const address = { + houseNumber: 42, + street: "Imaginary Road", + city: "Manchester", + country: "England", + postcode: "XYZ 123", +}; + +console.log(`My house number is ${address.houseNumber}`); + + + From 8e11a64fe998629f04e4fc4a4b4bd9575b2e5458 Mon Sep 17 00:00:00 2001 From: Khaliun Baatarkhuu Date: Wed, 22 Jul 2026 19:50:03 +0100 Subject: [PATCH 02/13] Fix iteration over author object using Object.values Updated loop to use Object.values for iterating over a plain object. --- Sprint-2/debug/author.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/Sprint-2/debug/author.js b/Sprint-2/debug/author.js index 8c2125977..33d9273f7 100644 --- a/Sprint-2/debug/author.js +++ b/Sprint-2/debug/author.js @@ -14,3 +14,19 @@ const author = { for (const value of author) { console.log(value); } + +// this code will throw an error because on line for 14 it is trying to loop a plain object. + +const author = { + firstName: "Zadie", + lastName: "Smith", + occupation: "writer", + age: 40, + alive: true, +}; + +for (const value of Object.values(author)) { + console.log(value); +} + +//we fix this by using Object.values, i.e. turning plain object in an array. From 3cd77220fd662ea07e8b8c300bf6c9a5f054f90e Mon Sep 17 00:00:00 2001 From: Khaliun Baatarkhuu Date: Wed, 22 Jul 2026 20:11:32 +0100 Subject: [PATCH 03/13] Fix recipe output formatting in console log Updated console log to display ingredients correctly. --- Sprint-2/debug/recipe.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/Sprint-2/debug/recipe.js b/Sprint-2/debug/recipe.js index 6cbdd22cd..933ae093e 100644 --- a/Sprint-2/debug/recipe.js +++ b/Sprint-2/debug/recipe.js @@ -13,3 +13,19 @@ const recipe = { console.log(`${recipe.title} serves ${recipe.serves} ingredients: ${recipe}`); + +//Prediction: +//output will be, [object Object]. Because recipe is an object, when js converts objects to a string (template literals can only insert strings) it calls on toString() method, which +//by default returns [Object Object]. We can fix this code by using join(\n) method. + +//fixed code: + +const recipe = { + title: "bruschetta", + serves: 2, + ingredients: ["olive oil", "tomatoes", "salt", "pepper"], +}; + +console.log(`${recipe.title} serves ${recipe.serves} +Ingredients: +${recipe.ingredients.join("\n")}`); From a7a69c584410e6e409458f67b2be13675039f432 Mon Sep 17 00:00:00 2001 From: Khaliun Baatarkhuu Date: Thu, 23 Jul 2026 08:30:38 +0100 Subject: [PATCH 04/13] Add tests for contains function behavior --- Sprint-2/implement/contains.test.js | 30 ++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/Sprint-2/implement/contains.test.js b/Sprint-2/implement/contains.test.js index 326bdb1f2..0097d8d69 100644 --- a/Sprint-2/implement/contains.test.js +++ b/Sprint-2/implement/contains.test.js @@ -17,19 +17,47 @@ as the object doesn't contains a key of 'c' // When passed an object and a property name // Then it should return true if the object contains the property, false otherwise +test("returns true if the object contains the property", () => { + const object = { a: 1, b: 2 }; + + expect(contains(object, "a")).toBe(true); +}); + +test("returns false if the object does not contain the property", () => { + const object = { a: 1, b: 2 }; + + expect(contains(object, "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("returns true when 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 property does not 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 an array", () => { + expect(contains(["a", "b"], "0")).toBe(false); +}); From 36513463558b50067b60568c93dcdca00b495bc7 Mon Sep 17 00:00:00 2001 From: Khaliun Baatarkhuu Date: Thu, 23 Jul 2026 08:31:22 +0100 Subject: [PATCH 05/13] Implement contains function to check object properties --- Sprint-2/implement/contains.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Sprint-2/implement/contains.js b/Sprint-2/implement/contains.js index cd779308a..f8d9f79ee 100644 --- a/Sprint-2/implement/contains.js +++ b/Sprint-2/implement/contains.js @@ -1,3 +1,9 @@ -function contains() {} +function contains(obj, property) { + if (typeof obj !== "object" || obj === null || Array.isArray(obj)) { + return false; + } + + return Object.hasOwn(obj, property); +} module.exports = contains; From 412d168ad266970b1b0819ebfd61a2a77b97be86 Mon Sep 17 00:00:00 2001 From: Khaliun Baatarkhuu Date: Thu, 23 Jul 2026 14:10:30 +0100 Subject: [PATCH 06/13] Modify createLookup to accept countryCurrencyPairs --- Sprint-2/implement/lookup.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/Sprint-2/implement/lookup.js b/Sprint-2/implement/lookup.js index a6746e07f..da5b177d4 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; From 7efd6808757a255a102d540706edc17615a00efb Mon Sep 17 00:00:00 2001 From: Khaliun Baatarkhuu Date: Thu, 23 Jul 2026 14:14:12 +0100 Subject: [PATCH 07/13] Implement test for country currency code lookup --- Sprint-2/implement/lookup.test.js | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/Sprint-2/implement/lookup.test.js b/Sprint-2/implement/lookup.test.js index 547e06c5a..a8ad2bbd2 100644 --- a/Sprint-2/implement/lookup.test.js +++ b/Sprint-2/implement/lookup.test.js @@ -1,6 +1,19 @@ 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", "GBP"], + ]; + const result = createLookup(countryCurrencyPairs); + + expect(result).toEqual({ + US: "USD", + CA: "CAD", + GB: "GBP", + }); +}); /* From 71ed164d4bea8699ce59163529d7bc57cf471736 Mon Sep 17 00:00:00 2001 From: Khaliun Baatarkhuu Date: Thu, 23 Jul 2026 22:42:25 +0100 Subject: [PATCH 08/13] Enhance parseQueryString to handle empty pairs --- Sprint-2/implement/querystring.js | 36 ++++++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/Sprint-2/implement/querystring.js b/Sprint-2/implement/querystring.js index 45ec4e5f3..de2902c8a 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; + // Ignore empty pairs + if (pair === "") continue; + + + const equalsIndex = pair.indexOf("="); + + let key, 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, " ")); + + + if (queryParams.hasOwnProperty(key)) { + if (Array.isArray(queryParams[key])) { + queryParams[key].push(value); + } else { + queryParams[key] = [queryParams[key], value]; + } + } else { + queryParams[key] = value; + } } return queryParams; From 9413fa5e13b4f5932fc8c6e17670277667cc20c2 Mon Sep 17 00:00:00 2001 From: Khaliun Baatarkhuu Date: Thu, 23 Jul 2026 22:44:01 +0100 Subject: [PATCH 09/13] Remove comment on empty query string pairs Removed comment about ignoring empty pairs for clarity. --- Sprint-2/implement/querystring.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sprint-2/implement/querystring.js b/Sprint-2/implement/querystring.js index de2902c8a..32daa79af 100644 --- a/Sprint-2/implement/querystring.js +++ b/Sprint-2/implement/querystring.js @@ -8,7 +8,7 @@ function parseQueryString(queryString) { const keyValuePairs = queryString.split("&"); for (const pair of keyValuePairs) { - // Ignore empty pairs + if (pair === "") continue; From 094c32b47b8cfdc4262906fae8639849e83470f5 Mon Sep 17 00:00:00 2001 From: Khaliun Baatarkhuu Date: Thu, 23 Jul 2026 22:46:38 +0100 Subject: [PATCH 10/13] Enhance query string tests for edge cases Added tests for handling empty query strings and decoding spaces. --- Sprint-2/implement/querystring.test.js | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/Sprint-2/implement/querystring.test.js b/Sprint-2/implement/querystring.test.js index 328b8df61..8f2ce6823 100644 --- a/Sprint-2/implement/querystring.test.js +++ b/Sprint-2/implement/querystring.test.js @@ -37,12 +37,23 @@ test("should replace '+' by ' '", () => { }); }); -// Stretch exercise: Handling query strings that contain identical keys +test("returns empty object for empty query", () => { + expect(parseQueryString("")).toEqual({}); +}); + +test("ignores multiple empty pairs", () => { + 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("decodes encoded spaces", () => { + expect(parseQueryString("name=John%20Doe")).toEqual({ + name: "John Doe", }); }); + +test("keeps all '=' in values", () => { + expect(parseQueryString("expr=a=b=c=d")).toEqual({ + expr: "a=b=c=d", + }); +}); + From 058fc4ed6221af03217e322e6d662a22972d1035 Mon Sep 17 00:00:00 2001 From: Khaliun Baatarkhuu Date: Thu, 23 Jul 2026 22:48:17 +0100 Subject: [PATCH 11/13] Implement tally function with input validation Add input validation and counting logic to tally function. --- Sprint-2/implement/tally.js | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/Sprint-2/implement/tally.js b/Sprint-2/implement/tally.js index f47321812..68af13a7a 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("Input must be an array"); + } + + const counts = {}; + + for (const item of items) { + if (counts[item]) { + counts[item]++; + } else { + counts[item] = 1; + } + } + + return counts; +} module.exports = tally; From 520c9fc894eb4d676fee1c3686ef3b5af351f6af Mon Sep 17 00:00:00 2001 From: Khaliun Baatarkhuu Date: Thu, 23 Jul 2026 22:52:09 +0100 Subject: [PATCH 12/13] Add tests for tally function with various inputs --- Sprint-2/implement/tally.test.js | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/Sprint-2/implement/tally.test.js b/Sprint-2/implement/tally.test.js index 2ceffa8dd..403b3fdd1 100644 --- a/Sprint-2/implement/tally.test.js +++ b/Sprint-2/implement/tally.test.js @@ -20,15 +20,40 @@ const tally = require("./tally.js"); // When passed an array of items // Then it should return an object containing the count for each unique item +test("returns an object containing the count for each unique item", () => { + expect(tally([1, 2, 2, 3, 3, 3])).toEqual({ + 1: 1, + 2: 2, + 3: 3, + }); +}); + // 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("counts duplicate items", () => { + 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("throws an error for invalid input", () => { + expect(() => tally("abc")).toThrow(); +}); + From 5a946c6560961974084a719533a570c92d0c414b Mon Sep 17 00:00:00 2001 From: Khaliun Baatarkhuu Date: Fri, 24 Jul 2026 08:48:04 +0100 Subject: [PATCH 13/13] Fix invert function implementation and add tests Implemented the invert function to swap keys and values of an object and added tests to verify its functionality. --- Sprint-2/interpret/invert.js | 49 ++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/Sprint-2/interpret/invert.js b/Sprint-2/interpret/invert.js index bb353fb1f..853ae6a9f 100644 --- a/Sprint-2/interpret/invert.js +++ b/Sprint-2/interpret/invert.js @@ -18,12 +18,61 @@ function invert(obj) { // 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 of an object's key-value pairs, and it is needed in this program +//so the for...of loop can access both each key and its corresponding value to swap them. + // d) Explain why the current return value is different from the target output +//It is different because invertObj.key creates a property literally calley "key". If we want to use the value of +//a variable as the property name, (invertedObj[value] = key) must be used instead. + // e) Fix the implementation of invert (and write tests to prove it's fixed!) + +function invert(obj) { + const invertedObj = {}; + + for (const [key, value] of Object.entries(obj)) { + invertedObj[value] = key; + } + + return invertedObj; +} + + + +describe("invert", () => { + test("inverts an empty object", () => { + expect(invert({})).toEqual({}); + }); + + test("inverts a single property", () => { + expect(invert({ a: 1 })).toEqual({ 1: "a" }); + }); + + test("inverts multiple properties", () => { + expect(invert({ a: 1, b: 2 })).toEqual({ + 1: "a", + 2: "b", + }); + }); + + test("works with other values", () => { + expect(invert({ x: 10, y: 20 })).toEqual({ + 10: "x", + 20: "y", + }); + }); +}); +