From 9fd1ab0e2049b36c50a7a484314f1cc5a166f35d Mon Sep 17 00:00:00 2001 From: Mansoor Munawar Date: Fri, 1 Aug 2025 03:20:35 +0100 Subject: [PATCH 01/11] Explained why there is bug in the program and also fixed it --- Sprint-2/debug/address.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Sprint-2/debug/address.js b/Sprint-2/debug/address.js index 940a6af83..b2475e804 100644 --- a/Sprint-2/debug/address.js +++ b/Sprint-2/debug/address.js @@ -1,4 +1,6 @@ // Predict and explain first... +// We create an object address which has 5 properties.In order to access the value within a property +// like house number we either need to use the dot notation such as address.houseNumber or square brackets such as address["houseNumber"] // This code should log out the houseNumber from the address object // but it 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"]}`); From e622cb5c4fc47ca0e733d187336d05dd2a2d180c Mon Sep 17 00:00:00 2001 From: Mansoor Munawar Date: Fri, 1 Aug 2025 15:31:35 +0100 Subject: [PATCH 02/11] fixed the bug in the program and explained why it occurs. --- Sprint-2/debug/author.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Sprint-2/debug/author.js b/Sprint-2/debug/author.js index 8c2125977..e23b111f8 100644 --- a/Sprint-2/debug/author.js +++ b/Sprint-2/debug/author.js @@ -1,5 +1,6 @@ // Predict and explain first... - +// it doesnt work as objects are not iteraable and there for we cant use for loops. +// But by using object.values allows us to get the value within an object and store it in an array and then we can use for of loop. // 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 +12,6 @@ const author = { alive: true, }; -for (const value of author) { +for (const value of Object.values(author)) { console.log(value); } From ce0ebff09d20c1aef15bff85bf2811c2c2720927 Mon Sep 17 00:00:00 2001 From: Mansoor Munawar Date: Fri, 1 Aug 2025 15:56:30 +0100 Subject: [PATCH 03/11] fixed the bug in the code and explained the reason for it --- Sprint-2/debug/recipe.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/Sprint-2/debug/recipe.js b/Sprint-2/debug/recipe.js index 6cbdd22cd..f0d905f32 100644 --- a/Sprint-2/debug/recipe.js +++ b/Sprint-2/debug/recipe.js @@ -1,5 +1,6 @@ // Predict and explain first... - +// there is a bug in this code, to access values of ingredients we need to use dot notation (recipe.ingredients) to access it values. +// In order to print out ingredients in seperate line we can then use for of loop. // 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? @@ -11,5 +12,7 @@ const recipe = { }; console.log(`${recipe.title} serves ${recipe.serves} - ingredients: -${recipe}`); +ingredients:`); +for (value of recipe.ingredients) { + console.log(value); +} From c0879fa45eeb435bcf6cb51a029954db57ec8389 Mon Sep 17 00:00:00 2001 From: Mansoor Munawar Date: Fri, 1 Aug 2025 21:44:17 +0100 Subject: [PATCH 04/11] implemented a function "contains" based on specific requirements and also created tests using jest for it --- Sprint-2/implement/contains.js | 5 ++++- Sprint-2/implement/contains.test.js | 29 ++++++++++++++++++++++++++--- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/Sprint-2/implement/contains.js b/Sprint-2/implement/contains.js index cd779308a..7f82e55a8 100644 --- a/Sprint-2/implement/contains.js +++ b/Sprint-2/implement/contains.js @@ -1,3 +1,6 @@ -function contains() {} +function contains(obj, key) { + return Object.hasOwn(obj, key); +} +console.log(contains([1, 2, 3, 4], "ingredients")); module.exports = contains; diff --git a/Sprint-2/implement/contains.test.js b/Sprint-2/implement/contains.test.js index 326bdb1f2..61730133f 100644 --- a/Sprint-2/implement/contains.test.js +++ b/Sprint-2/implement/contains.test.js @@ -20,16 +20,39 @@ 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 empty object returns false", () => { + const currentOutput = contains({}); + const targetOutput = false; + expect(currentOutput).toBe(targetOutput); +}); // 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 when passed to contains with existing propery name, it returns true", () => { + const currentOutput = contains( + { firstName: "Mansoor", organisation: "CYF" }, + "firstName" + ); + const targetOutput = true; + expect(currentOutput).toBe(targetOutput); +}); // 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 and when it passed to contains with non-existent property name then it should return false", () => { + const currentOutput = contains( + { firstName: "Mansoor", organisation: "CYF" }, + "Name" + ); + const targetOutput = false; + expect(currentOutput).toBe(targetOutput); +}); // Given invalid parameters like an array // When passed to contains // Then it should return false or throw an error +test("given invalid parameters like an array when passed to contains, it returns false or throw an error", () => { + const currentOutput = contains([1, 2, 3, 4], "hello"); + targetOutput = false; + expect(currentOutput).toBe(targetOutput); +}); From 2bc5d13f04e8326d7b4ab7dc16c9c13a70406d6b Mon Sep 17 00:00:00 2001 From: Mansoor Munawar Date: Fri, 1 Aug 2025 21:50:00 +0100 Subject: [PATCH 05/11] created an additional test for contains function --- Sprint-2/implement/contains.test.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/Sprint-2/implement/contains.test.js b/Sprint-2/implement/contains.test.js index 61730133f..8d46f4e8c 100644 --- a/Sprint-2/implement/contains.test.js +++ b/Sprint-2/implement/contains.test.js @@ -16,7 +16,15 @@ as the object doesn't contains a key of 'c' // 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("Given a contains function when passed an object and property name then it should false if the object doesnt contain the property", () => { + currentOutput = contains( + { firstName: "Mansoor", lastName: "Munawar" }, + "hello" + ); + targetOutput = false; + expect(currentOutput).toBe(targetOutput); +}); +// When passed an object and a property name") // Given an empty object // When passed to contains // Then it should return false From f3af6ca45a8c4acd7a392b152b64314d3c25dc5d Mon Sep 17 00:00:00 2001 From: Mansoor Munawar Date: Mon, 4 Aug 2025 17:55:43 +0100 Subject: [PATCH 06/11] created 'createLoopup' function based on the requirements and also created test for it by using Jest --- Sprint-2/implement/lookup.js | 14 ++++++++++++-- Sprint-2/implement/lookup.test.js | 12 +++++++++++- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/Sprint-2/implement/lookup.js b/Sprint-2/implement/lookup.js index a6746e07f..2c1d55157 100644 --- a/Sprint-2/implement/lookup.js +++ b/Sprint-2/implement/lookup.js @@ -1,5 +1,15 @@ -function createLookup() { +function createLookup(twoDeminsionalArray) { // implementation here + let result = {}; + for (let [country, currency] of twoDeminsionalArray) { + result[country] = currency; + } + return result; } - +console.log( + createLookup([ + ["US", "USD"], + ["CA", "CAD"], + ]) +); module.exports = createLookup; diff --git a/Sprint-2/implement/lookup.test.js b/Sprint-2/implement/lookup.test.js index 547e06c5a..56a27b49b 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", () => { + let currentOutput = createLookup([ + ["US", "USD"], + ["CA", "CAD"], + ]); + let targetOutput = { + US: "USD", + CA: "CAD", + }; + expect(currentOutput).toEqual(targetOutput); +}); /* From 91c79c880880cbd3526b18167af0ff35206f43f4 Mon Sep 17 00:00:00 2001 From: Mansoor Munawar Date: Mon, 4 Aug 2025 19:31:08 +0100 Subject: [PATCH 07/11] fixed my querystring function to take into account more edge cases and also created tests for it --- Sprint-2/implement/querystring.js | 12 +++++++++--- Sprint-2/implement/querystring.test.js | 16 ++++++++++++++-- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/Sprint-2/implement/querystring.js b/Sprint-2/implement/querystring.js index 45ec4e5f3..659bc93dc 100644 --- a/Sprint-2/implement/querystring.js +++ b/Sprint-2/implement/querystring.js @@ -6,11 +6,17 @@ function parseQueryString(queryString) { const keyValuePairs = queryString.split("&"); for (const pair of keyValuePairs) { - const [key, value] = pair.split("="); - queryParams[key] = value; + if (!pair.includes("=")) { + continue; + } + const [key, ...value] = pair.split("="); + if (key === "") { + continue; + } + queryParams[key] = value.join("="); } return queryParams; } - +console.log(parseQueryString("=sort&colour=yellow")); module.exports = parseQueryString; diff --git a/Sprint-2/implement/querystring.test.js b/Sprint-2/implement/querystring.test.js index 3e218b789..0f76c87b5 100644 --- a/Sprint-2/implement/querystring.test.js +++ b/Sprint-2/implement/querystring.test.js @@ -3,10 +3,22 @@ // 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("parses querystring with missing value", () => { + let currentOutput = parseQueryString("sort&colour=yellow"); + let targetOutput = { colour: "yellow" }; + expect(currentOutput).toEqual(targetOutput); +}); + +test("parses querystring with missing key", () => { + let currentOutput = parseQueryString("=dark&colour=yellow"); + let targetOutput = { colour: "yellow" }; + expect(currentOutput).toEqual(targetOutput); +}); From 9b981b8e01e44ac8ba25928228212cdd1fd31cb2 Mon Sep 17 00:00:00 2001 From: Mansoor Munawar Date: Thu, 7 Aug 2025 03:19:47 +0100 Subject: [PATCH 08/11] created function "tally" based on the requirement and also created tests for it using Jest --- Sprint-2/implement/tally.js | 16 +++++++++++++++- Sprint-2/implement/tally.test.js | 21 ++++++++++++++++++--- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/Sprint-2/implement/tally.js b/Sprint-2/implement/tally.js index f47321812..85db57b1e 100644 --- a/Sprint-2/implement/tally.js +++ b/Sprint-2/implement/tally.js @@ -1,3 +1,17 @@ -function tally() {} +function tally(arrayOfItems) { + if (!Array.isArray(arrayOfItems)) { + throw new TypeError("parameter must be an array"); + } + let result = {}; + arrayOfItems.forEach((element) => { + if (element in result) { + result[element] += 1; + } else { + result[element] = 1; + } + }); + return result; +} +console.log(tally([])); module.exports = tally; diff --git a/Sprint-2/implement/tally.test.js b/Sprint-2/implement/tally.test.js index 2ceffa8dd..fbf4b7224 100644 --- a/Sprint-2/implement/tally.test.js +++ b/Sprint-2/implement/tally.test.js @@ -19,16 +19,31 @@ 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("when function tally called with an array of items, it returns object containing the count for each unique item", () => { + let currentOutput = tally(["a", "a", "a"]); + let targetOutput = { a: 3 }; + expect(currentOutput).toEqual(targetOutput); +}); // 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", () => { + let currentOutput = tally([]); + let targetOutput = {}; + expect(currentOutput).toEqual(targetOutput); +}); // Given an array with duplicate items // When passed to tally // Then it should return counts for each unique item - +test("tally on array of duplicate items, it returns counts for each unique item", () => { + let currentOutput = tally(["a", "a", "b", "c"]); + let targetOutput = { a: 2, b: 1, c: 1 }; + expect(currentOutput).toEqual(targetOutput); +}); // Given an invalid input like a string // When passed to tally // Then it should throw an error +test("Given an invalid input like a string passed to tally, it returns an error", () => { + expect(() => tally("hello")).toThrow(TypeError); +}); From 3933f766c58992575c0509467c0a2da840651abc Mon Sep 17 00:00:00 2001 From: Mansoor Munawar Date: Thu, 7 Aug 2025 04:15:50 +0100 Subject: [PATCH 09/11] fixed the invert function and explained why previously it didnt work and also created tests for it --- Sprint-2/interpret/invert.js | 14 ++++++++------ Sprint-2/interpret/invert.test.js | 13 +++++++++++++ 2 files changed, 21 insertions(+), 6 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..936dab73d 100644 --- a/Sprint-2/interpret/invert.js +++ b/Sprint-2/interpret/invert.js @@ -10,20 +10,22 @@ function invert(obj) { const invertedObj = {}; for (const [key, value] of Object.entries(obj)) { - invertedObj.key = value; + invertedObj[value] = key; } return invertedObj; } +console.log(invert({ x: 10, y: 20 })); +module.exports = invert; // a) What is the current return value when invert is called with { a : 1 } - +// it will be {"key" : 1} // b) What is the current return value when invert is called with { a: 1, b: 2 } - +// it will be ["key": 2] // c) What is the target return value when invert is called with {a : 1, b: 2} - +// the target return value needs to be {"1": "a", "2": "b"} // c) What does Object.entries return? Why is it needed in this program? - +// object.entries return an array of each [key] and [value] pair and in this case it is necessary as we need both the key and value from the object. // d) Explain why the current return value is different from the target output - +// there is an bug as we use .key instead of [value]. So instead of taking the variable [value] we are instead creating a property called key which isnt correct. // 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..24bd4e796 --- /dev/null +++ b/Sprint-2/interpret/invert.test.js @@ -0,0 +1,13 @@ +const invert = require("./invert.js"); + +test("When invert is passed an object it should swap the keys and values in the object and then return it", () => { + let currentOutput = invert({ x: 10, y: 20 }); + let targetOutput = { 10: "x", 20: "y" }; + expect(currentOutput).toEqual(targetOutput); +}); + +test("When invert is passed an object it should swap the keys and values in the object and then return it", () => { + let currentOutput = invert({ a: 1, b: 2 }); + let targetOutput = { 1: "a", 2: "b" }; + expect(currentOutput).toEqual(targetOutput); +}); From 6425fff8480253c5926c1d2e6e7a5b899074c5e0 Mon Sep 17 00:00:00 2001 From: Mansoor Munawar Date: Fri, 15 Aug 2025 11:28:14 +0100 Subject: [PATCH 10/11] refactor contains function to handle 'length' key for arrays --- Sprint-2/implement/contains.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Sprint-2/implement/contains.js b/Sprint-2/implement/contains.js index 7f82e55a8..6de8901f9 100644 --- a/Sprint-2/implement/contains.js +++ b/Sprint-2/implement/contains.js @@ -1,6 +1,9 @@ function contains(obj, key) { + if (Array.isArray(obj) && key === "length") { + return false; + } return Object.hasOwn(obj, key); } -console.log(contains([1, 2, 3, 4], "ingredients")); +console.log(contains([1, 2, 3, 4], "length")); module.exports = contains; From 736be72891329b2a5e17bc15adb9aac47b048fcf Mon Sep 17 00:00:00 2001 From: Mansoor Munawar Date: Mon, 18 Aug 2025 10:43:07 +0100 Subject: [PATCH 11/11] refactor contains function to meet the test requirements --- Sprint-2/implement/contains.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Sprint-2/implement/contains.js b/Sprint-2/implement/contains.js index 6de8901f9..828bae56f 100644 --- a/Sprint-2/implement/contains.js +++ b/Sprint-2/implement/contains.js @@ -1,9 +1,9 @@ function contains(obj, key) { - if (Array.isArray(obj) && key === "length") { + if (Array.isArray(obj)) { return false; } return Object.hasOwn(obj, key); } -console.log(contains([1, 2, 3, 4], "length")); +console.log(contains([1, 2, 3, 4], "0")); module.exports = contains;