From 36332cfe0dc5b7ec5109a057d1e0b182e739d518 Mon Sep 17 00:00:00 2001 From: marthak1 Date: Sat, 21 Mar 2026 15:10:53 +0000 Subject: [PATCH 1/7] debug issues with access object's properties --- Sprint-2/debug/address.js | 4 ++-- Sprint-2/debug/author.js | 4 ++-- Sprint-2/debug/recipe.js | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/Sprint-2/debug/address.js b/Sprint-2/debug/address.js index 940a6af83..4502ed3c4 100644 --- a/Sprint-2/debug/address.js +++ b/Sprint-2/debug/address.js @@ -1,5 +1,5 @@ // Predict and explain first... - +// ${address[0]} is trying to access a property that does not exist // This code should log out the houseNumber from the address object // but it isn't working... // Fix anything that isn't working @@ -12,4 +12,4 @@ 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..093b3f5da 100644 --- a/Sprint-2/debug/author.js +++ b/Sprint-2/debug/author.js @@ -1,5 +1,5 @@ // Predict and explain first... - +// for...of is trying to iterate a non iterable object // 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 +11,6 @@ const author = { alive: true, }; -for (const value of author) { +for (const value in author) { console.log(value); } diff --git a/Sprint-2/debug/recipe.js b/Sprint-2/debug/recipe.js index 6cbdd22cd..124fa77ef 100644 --- a/Sprint-2/debug/recipe.js +++ b/Sprint-2/debug/recipe.js @@ -1,5 +1,5 @@ // Predict and explain first... - +// ${recipe} is trying to log an object as string and javascript does not have a default conversion of object to string it logs as [object object] // 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? @@ -12,4 +12,4 @@ const recipe = { console.log(`${recipe.title} serves ${recipe.serves} ingredients: -${recipe}`); +${recipe.ingredients.join('\n')}`); From a706aa1f7bd1dd8a4ca1892d517c7c027368144f Mon Sep 17 00:00:00 2001 From: marthak1 Date: Mon, 23 Mar 2026 19:32:18 +0000 Subject: [PATCH 2/7] debug code in files address, author and recipe.js --- Sprint-2/debug/address.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sprint-2/debug/address.js b/Sprint-2/debug/address.js index 4502ed3c4..127e45372 100644 --- a/Sprint-2/debug/address.js +++ b/Sprint-2/debug/address.js @@ -12,4 +12,4 @@ const address = { postcode: "XYZ 123", }; -console.log(`My house number is ${address[houseNumber]}`); +console.log(`My house number is ${address["houseNumber"]}`); From 57572df041569829a1ec88fcacefc06db15d7f46 Mon Sep 17 00:00:00 2001 From: marthak1 Date: Mon, 23 Mar 2026 23:32:46 +0000 Subject: [PATCH 3/7] implement function in contains.js and wrote test cases --- Sprint-2/implement/contains.js | 7 ++++++- Sprint-2/implement/contains.test.js | 29 ++++++++++++++++++++++++++++- 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/Sprint-2/implement/contains.js b/Sprint-2/implement/contains.js index cd779308a..0c739b3a3 100644 --- a/Sprint-2/implement/contains.js +++ b/Sprint-2/implement/contains.js @@ -1,3 +1,8 @@ -function contains() {} +function contains(obj, targetKey) { + if (obj.constructor !== Object) { + throw new Error("Invalid Parameter"); + } + return Object.keys(obj).includes(targetKey); +} module.exports = contains; diff --git a/Sprint-2/implement/contains.test.js b/Sprint-2/implement/contains.test.js index 326bdb1f2..2e32cc780 100644 --- a/Sprint-2/implement/contains.test.js +++ b/Sprint-2/implement/contains.test.js @@ -20,16 +20,43 @@ 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 input = {}; + const target = 'a' + const result = contains(input, target); + expect(result).toBe(false); + +}); // 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,it should return true", () => { + const input = { a: 1, b: 2 }; + const target = 'a' + const result = contains(input, target) + expect(result).toBe(true); +}); + // 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 a non-existent property name, it should return false", () => { + const input = { a: 1, b: 2 }; + const target = 'c' + const result = contains(input, target); + expect(result).toBe(false); +}); + // Given invalid parameters like an array // When passed to contains // Then it should return false or throw an error +test("given an invalid parameters, it should return false or throw an error", () => { + const input = ['a', 'b', 'c']; + const target = 'a' + expect(() => contains(input, target)).toThrow(); + +}); + From c84f128abb67768b802866f4363b6ca49dc8b330 Mon Sep 17 00:00:00 2001 From: marthak1 Date: Wed, 25 Mar 2026 18:30:56 +0000 Subject: [PATCH 4/7] implement and write test for lookup function --- Sprint-2/implement/lookup.js | 10 ++++++++-- Sprint-2/implement/lookup.test.js | 20 +++++++++++++++++++- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/Sprint-2/implement/lookup.js b/Sprint-2/implement/lookup.js index a6746e07f..acf66de80 100644 --- a/Sprint-2/implement/lookup.js +++ b/Sprint-2/implement/lookup.js @@ -1,5 +1,11 @@ -function createLookup() { - // implementation here +function createLookup(codeCurrencyArrays) { + const result = {}; + + for (const [code, currency] of codeCurrencyArrays) { + result[code] = currency; + } + + return result; } module.exports = createLookup; diff --git a/Sprint-2/implement/lookup.test.js b/Sprint-2/implement/lookup.test.js index 547e06c5a..3926e9696 100644 --- a/Sprint-2/implement/lookup.test.js +++ b/Sprint-2/implement/lookup.test.js @@ -1,6 +1,6 @@ const createLookup = require("./lookup.js"); -test.todo("creates a country currency code lookup for multiple codes"); +// test.todo("creates a country currency code lookup for multiple codes"); /* @@ -33,3 +33,21 @@ It should return: 'CA': 'CAD' } */ +test( + "given An array of arrays representing country code and currency code pairs, it should return an object where the keys are the country code", () => { + const input = [ + ["US", "USD"], + ["CA", "CAD"], + ["NG", "NGN"], + ["GP", "GBP"], + ]; + const result = createLookup(input); + expect(result).toStrictEqual({ + US: "USD", + CA: "CAD", + NG: "NGN", + GP: "GBP" + }) + } +); + From 7beca08c364b398e59c9499fca5a80b9db8ab788 Mon Sep 17 00:00:00 2001 From: marthak1 Date: Thu, 26 Mar 2026 18:55:58 +0000 Subject: [PATCH 5/7] implement and wrote test cases for parseQueryString function --- Sprint-2/implement/querystring.js | 9 ++++++++- Sprint-2/implement/querystring.test.js | 10 ++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/Sprint-2/implement/querystring.js b/Sprint-2/implement/querystring.js index 45ec4e5f3..5553f9ccb 100644 --- a/Sprint-2/implement/querystring.js +++ b/Sprint-2/implement/querystring.js @@ -6,7 +6,14 @@ function parseQueryString(queryString) { const keyValuePairs = queryString.split("&"); for (const pair of keyValuePairs) { - const [key, value] = pair.split("="); + const index = pair.indexOf("="); + + if (index === -1) { + queryParams[pair] = ""; + continue; + } + const key = pair.slice(0, index); + const value = pair.slice(index + 1); queryParams[key] = value; } diff --git a/Sprint-2/implement/querystring.test.js b/Sprint-2/implement/querystring.test.js index 3e218b789..9248fbafb 100644 --- a/Sprint-2/implement/querystring.test.js +++ b/Sprint-2/implement/querystring.test.js @@ -10,3 +10,13 @@ test("parses querystring values containing =", () => { "equation": "x=y+1", }); }); + + +test("given an empty querystring, it should return an empty object", () => { + expect(parseQueryString("")).toEqual({}) +}) + +test("given a querystring with missing value or no '=' found after the key, it should return empty string value", () => { + expect(parseQueryString("a=1&b=&c")).toEqual({ a: "1", b: "", c: "" }); +}); + From 52babe2e3cdaccc52e91a4aa1f309bdb6f448de1 Mon Sep 17 00:00:00 2001 From: marthak1 Date: Thu, 26 Mar 2026 21:37:45 +0000 Subject: [PATCH 6/7] implement and write test cases for tally function --- Sprint-2/implement/tally.js | 13 ++++++++++++- Sprint-2/implement/tally.test.js | 19 ++++++++++++++++++- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/Sprint-2/implement/tally.js b/Sprint-2/implement/tally.js index f47321812..4e0c7c3a4 100644 --- a/Sprint-2/implement/tally.js +++ b/Sprint-2/implement/tally.js @@ -1,3 +1,14 @@ -function tally() {} +function tally(list) { + const uniqueItems = {}; + if(list.constructor === String){ + throw new Error ("Invalid Input") + } + for (const frequency of list){ + uniqueItems[frequency] = (uniqueItems[frequency] || 0) + 1; + } + + return uniqueItems; +} + module.exports = tally; diff --git a/Sprint-2/implement/tally.test.js b/Sprint-2/implement/tally.test.js index 2ceffa8dd..94a86da64 100644 --- a/Sprint-2/implement/tally.test.js +++ b/Sprint-2/implement/tally.test.js @@ -19,16 +19,33 @@ 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("given an array of items, it should return an object containing count for each unique item", () => { + const input = ["a", "a", "a"]; + const result = tally(input); + const targetOutput = { a: 3 }; + expect(result).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", () => { +expect(tally([])).toEqual({}) +}); // Given an array with duplicate items // When passed to tally // Then it should return counts for each unique item +test("given an array of items, it should return an object containing count for each unique item", () => { + const input = ["a", "a", "b", "c"]; + const result = tally(input); + const targetOutput = { a: 2, b: 1, c: 1 }; + expect(result).toEqual(targetOutput); +}); // Given an invalid input like a string // When passed to tally // Then it should throw an error +test("", () => { + expect(() => tally("City Dreams")).toThrow(); +}); From 96d9df28d5c680d9b02922a23c84a75f6cb79fc4 Mon Sep 17 00:00:00 2001 From: marthak1 Date: Sat, 28 Mar 2026 15:03:55 +0000 Subject: [PATCH 7/7] fixed invert function to swap objcets proprties and wrote a passing test case --- Sprint-2/interpret/invert.js | 11 ++++++++++- Sprint-2/interpret/invert.test.js | 13 +++++++++++++ 2 files changed, 23 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 bb353fb1f..016e1d038 100644 --- a/Sprint-2/interpret/invert.js +++ b/Sprint-2/interpret/invert.js @@ -9,21 +9,30 @@ function invert(obj) { const invertedObj = {}; + for (const [key, value] of Object.entries(obj)) { - invertedObj.key = value; + invertedObj[value] = key; } return invertedObj; } +module.exports = invert; // a) What is the current return value when invert is called with { a : 1 } + // returns { key: 1 } // b) What is the current return value when invert is called with { a: 1, b: 2 } + // returns { key: 2 } // c) What is the target return value when invert is called with {a : 1, b: 2} + // should return {1: a, 2: b} // c) What does Object.entries return? Why is it needed in this program? +// Object.entries returns an array of a given object's own enumerable string-keyed property key-value pairs. +// It is needed to loop through entries in 'obj' // d) Explain why the current return value is different from the target output +// invertedObj.key = value; is creating a property named 'key' , to access the value stored in 'key' we use the bracket notation without quotes like this invertedObj[key]= value; If the property name comes from a variable → we always use bracket notation without quotes // e) Fix the implementation of invert (and write tests to prove it's fixed!) +// implementation fixed and test passed diff --git a/Sprint-2/interpret/invert.test.js b/Sprint-2/interpret/invert.test.js new file mode 100644 index 000000000..6a98f03dd --- /dev/null +++ b/Sprint-2/interpret/invert.test.js @@ -0,0 +1,13 @@ +const invert = require("./invert.js"); + +//Given a function invert() +//When passed an object +//Then it should return a swapped key and value in the object +// example: Given => {a: 1, b: 2, c: 3}, should return {1: a, 2: b, 3: c} + +test("Given an object with at least one key value pair, it should return an inverted key value", () => { + const input = { a: 1, b: 2, c: 3 }; + const output = invert(input); + const target = { "1": "a", "2": "b", "3": "c" }; + expect(output).toEqual(target); +})