From f84b6ff48f0e11ca1e60072da931701cb981a01a Mon Sep 17 00:00:00 2001 From: Nahom Mesfin Date: Fri, 15 Aug 2025 12:27:50 +0100 Subject: [PATCH 01/13] object key fix --- 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..d9fc08b5a 100644 --- a/Sprint-2/debug/address.js +++ b/Sprint-2/debug/address.js @@ -12,4 +12,6 @@ const address = { postcode: "XYZ 123", }; -console.log(`My house number is ${address[0]}`); +console.log(`My house number is ${address.houseNumber}`); + +//address is an object without a key, therefore address[0] is undefined. To fix this we write "address.houseNumber". \ No newline at end of file From cfc84ef5c7b7b1e56ffa5066a1f3dc8a596fdf3d Mon Sep 17 00:00:00 2001 From: Nahom Mesfin Date: Fri, 15 Aug 2025 12:28:44 +0100 Subject: [PATCH 02/13] author for...in loop --- Sprint-2/debug/author.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Sprint-2/debug/author.js b/Sprint-2/debug/author.js index 8c2125977..3df469f06 100644 --- a/Sprint-2/debug/author.js +++ b/Sprint-2/debug/author.js @@ -11,6 +11,7 @@ const author = { alive: true, }; -for (const value of author) { +for (const value in author) { console.log(value); } +// for...of works only with iterable objects like arrays, strings, maps..etc. In order to fix this problem we can use "for...in" to loop through the keys of the object. \ No newline at end of file From 889fa6d04afa1645e30d89a49309652a1a9387ce Mon Sep 17 00:00:00 2001 From: Nahom Mesfin Date: Fri, 15 Aug 2025 12:29:07 +0100 Subject: [PATCH 03/13] list recipe --- Sprint-2/debug/recipe.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/Sprint-2/debug/recipe.js b/Sprint-2/debug/recipe.js index 6cbdd22cd..5eaaf5f02 100644 --- a/Sprint-2/debug/recipe.js +++ b/Sprint-2/debug/recipe.js @@ -9,7 +9,9 @@ const recipe = { serves: 2, ingredients: ["olive oil", "tomatoes", "salt", "pepper"], }; +for(let ingredient of recipe.ingredients) -console.log(`${recipe.title} serves ${recipe.serves} - ingredients: -${recipe}`); +console.log(`${recipe.title} serves ${recipe.serves} ingredients: +${ingredient}`); + +//I used a for...of loop to list each ingredient on a new line. \ No newline at end of file From 713b7f6f89b37db9aaf62925234597fa923ed3aa Mon Sep 17 00:00:00 2001 From: Nahom Mesfin Date: Fri, 15 Aug 2025 12:53:59 +0100 Subject: [PATCH 04/13] contains key --- Sprint-2/implement/contains.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Sprint-2/implement/contains.js b/Sprint-2/implement/contains.js index cd779308a..d3c9971df 100644 --- a/Sprint-2/implement/contains.js +++ b/Sprint-2/implement/contains.js @@ -1,3 +1,8 @@ -function contains() {} +function contains(obj, propName) { + if (typeof obj !== "object" || obj === null || Array.isArray(obj)) { + return false; + } + return propName in obj; +} module.exports = contains; From f780923d8a1f0fbc3ffa5efb68a286ffc7634140 Mon Sep 17 00:00:00 2001 From: Nahom Mesfin Date: Fri, 15 Aug 2025 12:54:18 +0100 Subject: [PATCH 05/13] contains key test --- Sprint-2/implement/contains.test.js | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/Sprint-2/implement/contains.test.js b/Sprint-2/implement/contains.test.js index 326bdb1f2..204564de4 100644 --- a/Sprint-2/implement/contains.test.js +++ b/Sprint-2/implement/contains.test.js @@ -16,20 +16,39 @@ 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 +describe("contains", () => { + test("returns true if the object contains the property, false otherwise", () => { + expect(contains({CYF:3,ITP:5}, "ITP")).toBe(true); + }) // 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 for an existing property", () => { + expect(contains({ a: 1, b: 2 }, "a")).toBe(true); + expect(contains({ x: 10, y: 20 }, "y")).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 for a non-existent property", () => { + expect(contains({ a: 1, b: 2 }, "c")).toBe(false); + expect(contains({ x: 10, y: 20 }, "z")).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); + expect(contains(null, "a")).toBe(false); + expect(contains("string", "a")).toBe(false); + }); +}); \ No newline at end of file From 7449289a47841525789ef822fd99060dc53fed13 Mon Sep 17 00:00:00 2001 From: Nahom Mesfin Date: Fri, 15 Aug 2025 13:19:34 +0100 Subject: [PATCH 06/13] country-currency pair function --- Sprint-2/implement/lookup.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/Sprint-2/implement/lookup.js b/Sprint-2/implement/lookup.js index a6746e07f..3ed07e988 100644 --- a/Sprint-2/implement/lookup.js +++ b/Sprint-2/implement/lookup.js @@ -1,5 +1,9 @@ -function createLookup() { - // implementation here +function createLookup(pairs) { + const lookup = {}; + for (const [country, currency] of pairs) { + lookup[country] = currency + } + return lookup; } module.exports = createLookup; From 4da276b391ba67aa209008b9406ff3ce383ca84c Mon Sep 17 00:00:00 2001 From: Nahom Mesfin Date: Fri, 15 Aug 2025 13:20:23 +0100 Subject: [PATCH 07/13] test array - obj --- Sprint-2/implement/lookup.test.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/Sprint-2/implement/lookup.test.js b/Sprint-2/implement/lookup.test.js index 547e06c5a..a0f6e17e2 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", () => { + const input = [['US', 'USD'], ['CA', 'CAD'], ['JP', 'JPY']]; + const expected = { + US: 'USD', + CA: 'CAD', + JP: 'JPY' + }; + + expect(createLookup(input)).toEqual(expected); + }); + /* From 0764cfa9422c45b6d8f9f8f098b7e6daf99c006e Mon Sep 17 00:00:00 2001 From: Nahom Mesfin Date: Sat, 16 Aug 2025 14:26:10 +0100 Subject: [PATCH 08/13] querystring function --- Sprint-2/implement/querystring.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Sprint-2/implement/querystring.js b/Sprint-2/implement/querystring.js index 45ec4e5f3..84b6a41fa 100644 --- a/Sprint-2/implement/querystring.js +++ b/Sprint-2/implement/querystring.js @@ -6,7 +6,9 @@ function parseQueryString(queryString) { const keyValuePairs = queryString.split("&"); for (const pair of keyValuePairs) { - const [key, value] = pair.split("="); + const index = pair.indexOf("="); + const key = pair.slice(0, index); + const value = pair.slice(index + 1); queryParams[key] = value; } From 0b400549db90248237d8339cb630b3a069a1e80f Mon Sep 17 00:00:00 2001 From: Nahom Mesfin Date: Sat, 16 Aug 2025 14:26:29 +0100 Subject: [PATCH 09/13] querystring testcases --- Sprint-2/implement/querystring.test.js | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/Sprint-2/implement/querystring.test.js b/Sprint-2/implement/querystring.test.js index 3e218b789..7dae339b2 100644 --- a/Sprint-2/implement/querystring.test.js +++ b/Sprint-2/implement/querystring.test.js @@ -5,8 +5,22 @@ const parseQueryString = require("./querystring.js") -test("parses querystring values containing =", () => { - expect(parseQueryString("equation=x=y+1")).toEqual({ - "equation": "x=y+1", +describe("querystring parser", () => { + test("parses querystring values containing =", () => { + expect(parseQueryString("equation=x=y+1")).toEqual({ + "equation": "x=y+1", + }); }); -}); + test("returns empty object for empty query string", () => { + expect(parseQueryString("")).toEqual({}); + }); + test("handles key with missing value", () => { + expect(parseQueryString("key=")).toEqual({ key: "" }); + }); + test("handles missing key with value", () => { + expect(parseQueryString("=value")).toEqual({ "": "value" }); + }); + test("handles multiple key-value pairs", () => { + expect(parseQueryString("a=1&b=2")).toEqual({ a: "1", b: "2" }); + }); + }) \ No newline at end of file From 29cdd0993cf5b5c497f73f60d9d89b23e2464d4c Mon Sep 17 00:00:00 2001 From: Nahom Mesfin Date: Sat, 16 Aug 2025 14:26:40 +0100 Subject: [PATCH 10/13] tally function --- Sprint-2/implement/tally.js | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/Sprint-2/implement/tally.js b/Sprint-2/implement/tally.js index f47321812..c3192d498 100644 --- a/Sprint-2/implement/tally.js +++ b/Sprint-2/implement/tally.js @@ -1,3 +1,17 @@ -function tally() {} +function tally(arr) { + if (!Array.isArray(arr)) { + throw new Error("Input must be an array"); + } + + const result = {}; + for (const item of arr) { + if (result[item] === undefined) { + result[item] = 1; + } else { + result[item] += 1; + } + } + return result; +} module.exports = tally; From fab29c213f381b48e69442b56c76301fecae999a Mon Sep 17 00:00:00 2001 From: Nahom Mesfin Date: Sat, 16 Aug 2025 14:26:51 +0100 Subject: [PATCH 11/13] tally test --- Sprint-2/implement/tally.test.js | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/Sprint-2/implement/tally.test.js b/Sprint-2/implement/tally.test.js index 2ceffa8dd..19267fa3f 100644 --- a/Sprint-2/implement/tally.test.js +++ b/Sprint-2/implement/tally.test.js @@ -19,16 +19,28 @@ 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 - +describe("tally test", () => { +test("tally counts each unique item correctly", () => { + expect(tally(["a", "b", "c"])).toEqual({ a: 1, b: 1, c: 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("tally counts each unique item correctly", () => { + 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("tally throws an error for invalid input", () => { + expect(() => tally("CYF")).toThrow("Input must be an array"); +}); +}); \ No newline at end of file From b45be0a3d657668bed3ab2253f59c67e7c91c43a Mon Sep 17 00:00:00 2001 From: Nahom Mesfin Date: Sat, 16 Aug 2025 14:27:31 +0100 Subject: [PATCH 12/13] invert key with value and test --- Sprint-2/interpret/invert.js | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/Sprint-2/interpret/invert.js b/Sprint-2/interpret/invert.js index bb353fb1f..2f5f84497 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; + 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? - +// Object.entries gives an array of [key, value] pairs. // d) Explain why the current return value is different from the target output - +// invertedObj.key sets a property literally called "key". We need to use the variable key as the dynamic property name. and we need to change "key" to "value" and "value" to "key". // e) Fix the implementation of invert (and write tests to prove it's fixed!) +console.assert( + JSON.stringify(invert({ a: 1, b: 2 })) === JSON.stringify({ "1": "a", "2": "b" }), + "multiple keys test failed" +); +console.assert( + JSON.stringify(invert({ x: "hello", y: "world" })) === JSON.stringify({ "hello": "x", "world": "y" }), + "string cases test failed" +); +console.assert( + JSON.stringify(invert({})) === JSON.stringify({}), + "empty test failed" +); +console.log("All tests passed!"); From b4756cd4657393ddd438bb74d682791000f6cc54 Mon Sep 17 00:00:00 2001 From: Nahom Mesfin Date: Mon, 18 Aug 2025 12:39:29 +0100 Subject: [PATCH 13/13] fix ingredients --- Sprint-2/debug/recipe.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Sprint-2/debug/recipe.js b/Sprint-2/debug/recipe.js index 5eaaf5f02..76714b2c7 100644 --- a/Sprint-2/debug/recipe.js +++ b/Sprint-2/debug/recipe.js @@ -9,9 +9,10 @@ const recipe = { serves: 2, ingredients: ["olive oil", "tomatoes", "salt", "pepper"], }; -for(let ingredient of recipe.ingredients) -console.log(`${recipe.title} serves ${recipe.serves} ingredients: -${ingredient}`); +console.log(`${recipe.title} serves ${recipe.serves} ingredients:`); +for(let ingredient of recipe.ingredients) { + console.log(ingredient) +} //I used a for...of loop to list each ingredient on a new line. \ No newline at end of file