From 28a696fb75a585c0a0b6845ebd495d3edf2c06a2 Mon Sep 17 00:00:00 2001 From: Yonatan Teklemariam Date: Tue, 7 Jul 2026 07:29:16 +0100 Subject: [PATCH 1/5] reverted to original --- Sprint-1/fix/median.js | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index b22590bc6..a1e52d497 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -6,9 +6,31 @@ // or 'list' has mixed values (the function is expected to sort only numbers). function calculateMedian(list) { - const middleIndex = Math.floor(list.length / 2); - const median = list.splice(middleIndex, 1)[0]; - return median; + if (!Array.isArray(list)) { + return null; + } + if (list.length === 0) { + return null; + } + const numbersOnly = list.filter((item) => typeof item === "number"); + if (numbersOnly.length === 0) { + return null; + } + const sorted = [...numbersOnly].sort((a, b) => a - b); + const middleIndex = Math.floor(sorted.length / 2); + if (sorted.length % 2 === 0) { + const median = (sorted[middleIndex - 1] + sorted[middleIndex]) / 2; + return median; + } + return sorted[middleIndex]; } +/* +const middleIndex = Math.floor(list.length / 2); +const median = list.splice(middleIndex, 1)[0]; +return median; +*/ module.exports = calculateMedian; +/* + + */ From cff19af51abaccc5438b6f625a4565711f1ac370 Mon Sep 17 00:00:00 2001 From: Yonatan Teklemariam Date: Tue, 7 Jul 2026 07:45:43 +0100 Subject: [PATCH 2/5] reverted to original --- Sprint-1/fix/median.js | 28 +++------------------------- 1 file changed, 3 insertions(+), 25 deletions(-) diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index a1e52d497..b22590bc6 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -6,31 +6,9 @@ // or 'list' has mixed values (the function is expected to sort only numbers). function calculateMedian(list) { - if (!Array.isArray(list)) { - return null; - } - if (list.length === 0) { - return null; - } - const numbersOnly = list.filter((item) => typeof item === "number"); - if (numbersOnly.length === 0) { - return null; - } - const sorted = [...numbersOnly].sort((a, b) => a - b); - const middleIndex = Math.floor(sorted.length / 2); - if (sorted.length % 2 === 0) { - const median = (sorted[middleIndex - 1] + sorted[middleIndex]) / 2; - return median; - } - return sorted[middleIndex]; + const middleIndex = Math.floor(list.length / 2); + const median = list.splice(middleIndex, 1)[0]; + return median; } -/* -const middleIndex = Math.floor(list.length / 2); -const median = list.splice(middleIndex, 1)[0]; -return median; -*/ module.exports = calculateMedian; -/* - - */ From 7f760a0134a769739a55c5c8cb9d2a5d1290fe3a Mon Sep 17 00:00:00 2001 From: Yonatan Teklemariam Date: Wed, 8 Jul 2026 19:38:59 +0100 Subject: [PATCH 3/5] debugged the three functions and fixed them --- Sprint-2/debug/recipe.js | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/Sprint-2/debug/recipe.js b/Sprint-2/debug/recipe.js index 6cbdd22cd..571daee80 100644 --- a/Sprint-2/debug/recipe.js +++ b/Sprint-2/debug/recipe.js @@ -9,7 +9,14 @@ const recipe = { serves: 2, ingredients: ["olive oil", "tomatoes", "salt", "pepper"], }; - -console.log(`${recipe.title} serves ${recipe.serves} - ingredients: -${recipe}`); +let recipeEntries = Object.entries(recipe); +for (const [reicpeKey, recipeValue] of recipeEntries) { + if (reicpeKey === "ingredients") { + console.log("Ingredients:"); + for (const ingredient of recipeValue) { + console.log(ingredient); + } + } else { + console.log(`${reicpeKey}: ${recipeValue}`); + } +} From 7ed691127c1fdaa567ef80497411f8d750285574 Mon Sep 17 00:00:00 2001 From: Yonatan Teklemariam Date: Wed, 8 Jul 2026 19:46:41 +0100 Subject: [PATCH 4/5] added some comments for highlighting purposes. --- Sprint-2/debug/address.js | 3 ++- Sprint-2/debug/author.js | 5 ++++- Sprint-2/debug/recipe.js | 2 +- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/Sprint-2/debug/address.js b/Sprint-2/debug/address.js index 940a6af83..80c42de5f 100644 --- a/Sprint-2/debug/address.js +++ b/Sprint-2/debug/address.js @@ -12,4 +12,5 @@ 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..39604252c 100644 --- a/Sprint-2/debug/author.js +++ b/Sprint-2/debug/author.js @@ -2,6 +2,7 @@ // 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 +// Because objects are not iterable, so they need to be converted to arrays so as we can loop through them so, with Object.entries() method as this method converts the objects into key and value pairs within an array. const author = { firstName: "Zadie", @@ -11,6 +12,8 @@ const author = { alive: true, }; -for (const value of author) { +let authorEntries = Object.entries(author); +for (const value of authorEntries) { console.log(value); } +// diff --git a/Sprint-2/debug/recipe.js b/Sprint-2/debug/recipe.js index 571daee80..8c796cbf2 100644 --- a/Sprint-2/debug/recipe.js +++ b/Sprint-2/debug/recipe.js @@ -1,5 +1,5 @@ // Predict and explain first... - +// so the first recipe.title prints the correct title, the second recipe.serves also prints the correct number of serves but the last recipe in template literals will throw error or perhaps undefined as recipe is an object and needs different method to access it. so basically inorder to access the key and values inside an object, it should be accessed by using object name followed by a dot and one of the values or keys inside it (whatever we want to use). // 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? From 6ba6fdc2356554d99c425ea5adc5ea6248660e25 Mon Sep 17 00:00:00 2001 From: Yonatan Teklemariam Date: Wed, 8 Jul 2026 20:35:42 +0100 Subject: [PATCH 5/5] implemented the function and wrote the tests, re-run the test to confirm if it is working. all checked passed. --- Sprint-2/implement/contains.js | 9 ++++++++- Sprint-2/implement/contains.test.js | 20 +++++++++++++++++--- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/Sprint-2/implement/contains.js b/Sprint-2/implement/contains.js index cd779308a..8ed7ef395 100644 --- a/Sprint-2/implement/contains.js +++ b/Sprint-2/implement/contains.js @@ -1,3 +1,10 @@ -function contains() {} +function contains(obj, key) { + for (const currentKey of Object.keys(obj)) { + if (currentKey === key) { + return true; + } + } + return false; +} module.exports = contains; diff --git a/Sprint-2/implement/contains.test.js b/Sprint-2/implement/contains.test.js index 326bdb1f2..2f02912f1 100644 --- a/Sprint-2/implement/contains.test.js +++ b/Sprint-2/implement/contains.test.js @@ -10,26 +10,40 @@ 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' */ +console.log("THIS IS THE REAL contains.js FILE"); // 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 both existent and non-existent properties, returns true if existent or false if otherwise", () => { + expect(contains({ z: 2, e: 5 }, "a")).toBe(false); + expect(contains({ y: 6, x: 3, s: 5 }, "s")).toBe(true); +}); // Given an empty object // When passed to contains // Then it should return false -test.todo("contains on empty object returns false"); +test("returns false for an empty object", () => { + 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 object contains the property", () => { + expect(contains({ a: 1, b: 3 }, "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 object does not contain the property", () => { + expect(contains({ w: 1, x: 8 }, "r")).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 invalid parameters like an array", () => { + expect(contains([], "a")).toBe(false); +});