From 983314341a80e1a97eea14ec51e4e113f97e760e Mon Sep 17 00:00:00 2001 From: AjueborChukwuemeke Date: Fri, 15 Aug 2025 06:34:53 +0100 Subject: [PATCH 1/5] Sprint-2_debug --- Sprint-2/debug/address.js | 4 +++- Sprint-2/debug/author.js | 7 +++++-- Sprint-2/debug/recipe.js | 8 ++++++-- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/Sprint-2/debug/address.js b/Sprint-2/debug/address.js index 940a6af83..b69e4bd00 100644 --- a/Sprint-2/debug/address.js +++ b/Sprint-2/debug/address.js @@ -4,6 +4,8 @@ // but it isn't working... // Fix anything that isn't working +// it won't work because this is an object literal with key-value pairs + const address = { houseNumber: 42, street: "Imaginary Road", @@ -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"]}`); diff --git a/Sprint-2/debug/author.js b/Sprint-2/debug/author.js index 8c2125977..05b58fdcd 100644 --- a/Sprint-2/debug/author.js +++ b/Sprint-2/debug/author.js @@ -11,6 +11,9 @@ const author = { alive: true, }; -for (const value of author) { - console.log(value); +for (const val in author) { + console.log(author[val]); } +// Changing value to val so it does not conflict with fact that it is the keys that are being logged to +// display the 'value' they hold +//Used for .... in as this will iterate over the keys ie the indexes rather than the values diff --git a/Sprint-2/debug/recipe.js b/Sprint-2/debug/recipe.js index 6cbdd22cd..6b5d53d28 100644 --- a/Sprint-2/debug/recipe.js +++ b/Sprint-2/debug/recipe.js @@ -11,5 +11,9 @@ const recipe = { }; console.log(`${recipe.title} serves ${recipe.serves} - ingredients: -${recipe}`); + ingredients:`); + for (const items of recipe.ingredients) { + console.log(items); +}; +//we have to use for...of as it will loop directly over values of the array +// rather than the for ... in which will loop over the indexes. From ddd37f6c9c40ac12d01f597a92acb88ee79d3c1c Mon Sep 17 00:00:00 2001 From: AjueborChukwuemeke Date: Sat, 16 Aug 2025 11:21:57 +0100 Subject: [PATCH 2/5] implement_contain_lookup --- Sprint-2/implement/contains.js | 14 +++++++++++++- Sprint-2/implement/contains.test.js | 18 ++++++++++++++++++ Sprint-2/implement/lookup.js | 13 +++++++++++-- Sprint-2/implement/lookup.test.js | 20 +++++++++++++++++++- 4 files changed, 61 insertions(+), 4 deletions(-) diff --git a/Sprint-2/implement/contains.js b/Sprint-2/implement/contains.js index cd779308a..b52bdc68c 100644 --- a/Sprint-2/implement/contains.js +++ b/Sprint-2/implement/contains.js @@ -1,3 +1,15 @@ -function contains() {} +function contains(obj,item) { + if (obj === null || typeof obj !== 'object' || Array.isArray(obj)) { + return false; + } + + if (typeof item !== 'string') { + return false; + } + + // this checks that the property exists in the object + return obj.hasOwnProperty(item); +} + module.exports = contains; diff --git a/Sprint-2/implement/contains.test.js b/Sprint-2/implement/contains.test.js index 326bdb1f2..acaae9ec7 100644 --- a/Sprint-2/implement/contains.test.js +++ b/Sprint-2/implement/contains.test.js @@ -33,3 +33,21 @@ test.todo("contains on empty object returns false"); // Given invalid parameters like an array // When passed to contains // Then it should return false or throw an error + +describe('contains', () => { + test('returns false when given an empty object', () => { + expect(contains({}, 'a')).toBe(false); + }); + + test('returns true if the object has the property', () => { + expect(contains({ a: 1, b: 2, c: 3, d: 4 }, 'a')).toBe(true); + }); + + test('returns false if the object does not have the property', () => { + expect(contains({ a: 1, b: 2, c: 3 }, 'e')).toBe(false); + }); + + test('should return false when passed an array', () => { + expect(contains([1, 2, 3], 'a')).toBe(false); + }); +}); \ No newline at end of file diff --git a/Sprint-2/implement/lookup.js b/Sprint-2/implement/lookup.js index a6746e07f..79400e56c 100644 --- a/Sprint-2/implement/lookup.js +++ b/Sprint-2/implement/lookup.js @@ -1,5 +1,14 @@ -function createLookup() { - // implementation here +function createLookup(countryCurrencyPairs) { + let lookup = {}; //begins with an empty object to populate + + for (let pair of countryCurrencyPairs) { + let country = pair[0]; + let currency = pair[1]; + + lookup[country] = currency; + } +//this iterates through all pairs returning them into the created empty object + return lookup; } module.exports = createLookup; diff --git a/Sprint-2/implement/lookup.test.js b/Sprint-2/implement/lookup.test.js index 547e06c5a..1a2f17a25 100644 --- a/Sprint-2/implement/lookup.test.js +++ b/Sprint-2/implement/lookup.test.js @@ -1,6 +1,24 @@ 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 pairs = [ + ['US', 'USD'], + ['CA', 'CAD'], + ['GB', 'GBP'], + ['JP', 'JPY'], + ['IN', 'INR'], + ]; + + const expected = { + US: 'USD', + CA: 'CAD', + GB: 'GBP', + JP: 'JPY', + IN: 'INR', + }; + + expect(createLookup(pairs)).toEqual(expected); +}); /* From 34a3801cb004d4a4f8f1e4309499ee483ff2996c Mon Sep 17 00:00:00 2001 From: AjueborChukwuemeke Date: Sat, 16 Aug 2025 18:10:52 +0100 Subject: [PATCH 3/5] implement_tally --- Sprint-2/implement/tally.js | 20 +++++++++++++++++++- Sprint-2/implement/tally.test.js | 28 +++++++++++++++++++++++++++- 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/Sprint-2/implement/tally.js b/Sprint-2/implement/tally.js index f47321812..58199bc7e 100644 --- a/Sprint-2/implement/tally.js +++ b/Sprint-2/implement/tally.js @@ -1,3 +1,21 @@ -function tally() {} +function tally(arr) { + if (!Array.isArray(arr)) { + throw new Error("Input an array!"); + } + + const counts = {}; + + for (let i = 0; i < arr.length; i++) { + const item = arr[i]; + if (counts[item]) { + counts[item] += 1; + } else { + counts[item] = 1; + }// if/else ensures add to the count of an item if it has been + // counted in a previous iteration or start at 1. + } + + return counts; +} module.exports = tally; diff --git a/Sprint-2/implement/tally.test.js b/Sprint-2/implement/tally.test.js index 2ceffa8dd..5e3966c1a 100644 --- a/Sprint-2/implement/tally.test.js +++ b/Sprint-2/implement/tally.test.js @@ -23,7 +23,7 @@ const tally = require("./tally.js"); // 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.todo("tally on an empty array returns an empty object"); // Given an array with duplicate items // When passed to tally @@ -32,3 +32,29 @@ test.todo("tally on an empty array returns an empty object"); // Given an invalid input like a string // When passed to tally // Then it should throw an error + +describe("tally function", () => { + test("tally on an empty array returns an empty object", () => { + expect(tally([])).toEqual({}); + }); + + test("tally on an array with one item", () => { + expect(tally(["a"])).toEqual({ a: 1 }); + }); + + test("tally on an array with duplicate items", () => { + expect(tally(["a", "a", "a"])).toEqual({ a: 3 }); + }); + + test("tally on an array with multiple different items", () => { + expect(tally(["a", "a", "a", "b", "b", "c"])).toEqual({ a: 3, b: 2, c: 1 }); + }); + + test("tally throws an error for a string input", () => { + expect(() => tally("not an array")).toThrow("Input an array!"); + }); + + test("tally throws an error for an object input", () => { + expect(() => tally({ a: 1 })).toThrow("Input an array!"); + }); +}); \ No newline at end of file From ec864238b58fbf478a57eb6d1fddc2b37bddb5bd Mon Sep 17 00:00:00 2001 From: AjueborChukwuemeke Date: Sat, 16 Aug 2025 19:10:00 +0100 Subject: [PATCH 4/5] implement_querystring --- Sprint-2/implement/querystring.js | 17 +++++++++++++++-- Sprint-2/implement/querystring.test.js | 25 +++++++++++++++++++++++-- 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/Sprint-2/implement/querystring.js b/Sprint-2/implement/querystring.js index 45ec4e5f3..c3520d82f 100644 --- a/Sprint-2/implement/querystring.js +++ b/Sprint-2/implement/querystring.js @@ -5,8 +5,21 @@ function parseQueryString(queryString) { } const keyValuePairs = queryString.split("&"); - for (const pair of keyValuePairs) { - const [key, value] = pair.split("="); + for (let i = 0; i < keyValuePairs.length; i++) { + const pair = keyValuePairs[i]; + const equalIndex = pair.indexOf("="); + + let key, value; + + if (equalIndex === -1) { + key = pair; + value = ""; + } // if no "=" is found key is assigned with a value of empty string + else { + key = pair.substring(0, equalIndex); + value = pair.substring(equalIndex + 1); + } // Split into key and value for as many "=" in the value using substring + queryParams[key] = value; } diff --git a/Sprint-2/implement/querystring.test.js b/Sprint-2/implement/querystring.test.js index 3e218b789..e78606a6a 100644 --- a/Sprint-2/implement/querystring.test.js +++ b/Sprint-2/implement/querystring.test.js @@ -6,7 +6,28 @@ const parseQueryString = require("./querystring.js") test("parses querystring values containing =", () => { - expect(parseQueryString("equation=x=y+1")).toEqual({ - "equation": "x=y+1", + expect(parseQueryString("equation=x=y+1")).toEqual({"equation": "x=y+1"}) +}); +test("returns empty object for empty query string", () => { + expect(parseQueryString("")).toEqual({}); +}); + +test("parses single key=value pair", () => { + expect(parseQueryString("a=1")).toEqual({ a: "1" }); +}); + +test("parses multiple key=value pairs", () => { + expect(parseQueryString("a=1&b=2&c=3")).toEqual({ + a: "1", + b: "2", + c: "3", }); }); + +test("parses key without value (assigns empty string)", () => { + expect(parseQueryString("a=&b=2")).toEqual({a: "", b: "2"}); +}); + +test("handles key with no '=' at all", () => { + expect(parseQueryString("hello")).toEqual({hello: ""}); +}); From 19fa53c1050aac5154888462062296a2a37b07b9 Mon Sep 17 00:00:00 2001 From: AjueborChukwuemeke Date: Sat, 16 Aug 2025 20:33:26 +0100 Subject: [PATCH 5/5] interpret_invert --- Sprint-2/implement/contains.test.js | 1 - Sprint-2/interpret/invert.js | 13 ++++++++++++- Sprint-2/interpret/invert.test.js | 9 +++++++++ 3 files changed, 21 insertions(+), 2 deletions(-) create mode 100644 Sprint-2/interpret/invert.test.js diff --git a/Sprint-2/implement/contains.test.js b/Sprint-2/implement/contains.test.js index acaae9ec7..6aeafe925 100644 --- a/Sprint-2/implement/contains.test.js +++ b/Sprint-2/implement/contains.test.js @@ -20,7 +20,6 @@ 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"); // Given an object with properties // When passed to contains with an existing property name diff --git a/Sprint-2/interpret/invert.js b/Sprint-2/interpret/invert.js index bb353fb1f..17901d7a6 100644 --- a/Sprint-2/interpret/invert.js +++ b/Sprint-2/interpret/invert.js @@ -10,20 +10,31 @@ 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 } + // the current return will take the value in the key value pair using Object.entries and + // and assign it to a property called key returning {key:1} // b) What is the current return value when invert is called with { a: 1, b: 2 } + //after the first loop, second iteration reassigns the value to + //the property called key returning {key:2} // c) What is the target return value when invert is called with {a : 1, b: 2} + // it should give {"1": "a", "2": "b" } // c) What does Object.entries return? Why is it needed in this program? + //this function allows for iteration over both keys and values at once + // instead of looping over them seperately. // d) Explain why the current return value is different from the target output + //for the target output, we want the property name to be the set to the value + // of the original key-value pair. // 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..8a0ed0d74 --- /dev/null +++ b/Sprint-2/interpret/invert.test.js @@ -0,0 +1,9 @@ +const invert = require("./invert.js"); + +test("inverts a single key-value pair", () => { + expect(invert({ a: 1 })).toEqual({ "1": "a" }); +}); + +test("inverts multiple unique key-value pairs", () => { + expect(invert({ x: 3, y: 4 })).toEqual({ "3": "x", "4": "y" }); +});