From c9145dfc0a9be944a44642f290176cd6a3fffe52 Mon Sep 17 00:00:00 2001 From: russom Date: Thu, 16 Jul 2026 20:57:19 +0100 Subject: [PATCH 01/29] CalculateMedian function implemented --- Sprint-1/fix/median.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index b22590bc6..8b942a384 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -6,9 +6,16 @@ // or 'list' has mixed values (the function is expected to sort only numbers). function calculateMedian(list) { + for (let i = 0; i < list.length; i++) { + if (typeof list[i] === "number" && typeof list[i] === " ") { + return null; + } const middleIndex = Math.floor(list.length / 2); const median = list.splice(middleIndex, 1)[0]; return median; } +} +let nums = [1, 2, 3]; +console.log(calculateMedian(nums)); module.exports = calculateMedian; From 1cf01db33917126bc42de35c6a6f4768590f6276 Mon Sep 17 00:00:00 2001 From: russom Date: Thu, 16 Jul 2026 20:58:11 +0100 Subject: [PATCH 02/29] Comment updated on return null --- Sprint-1/fix/median.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index 8b942a384..822c398b5 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -7,8 +7,8 @@ function calculateMedian(list) { for (let i = 0; i < list.length; i++) { - if (typeof list[i] === "number" && typeof list[i] === " ") { - return null; + if (typeof list[i] === "number" && typeof list[i] === " ") { // List is checked whether it is numbers and strings. + return null; // Null is returned if list is not numbers or has mixed values(numbers and strings) } const middleIndex = Math.floor(list.length / 2); const median = list.splice(middleIndex, 1)[0]; From 256ef50be642483dee5284a511cdcdc005e71358 Mon Sep 17 00:00:00 2001 From: russom Date: Thu, 16 Jul 2026 23:01:36 +0100 Subject: [PATCH 03/29] Function calculateMedian updated so it could calculate even elements of arrays. --- Sprint-1/fix/median.js | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index 822c398b5..561e70090 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -7,15 +7,17 @@ function calculateMedian(list) { for (let i = 0; i < list.length; i++) { - if (typeof list[i] === "number" && typeof list[i] === " ") { // List is checked whether it is numbers and strings. - return null; // Null is returned if list is not numbers or has mixed values(numbers and strings) + if (typeof list[i] === "number" && typeof list[i] === " ") { + // List is checked whether it is numbers and strings. + return null; // Null is returned if list is not numbers or has mixed values(numbers and strings) + } + const middleIndex = Math.floor(list.length / 2); + + if (list.length % 2 === 0) { + return (list[middleIndex - 1] + list[middleIndex]) / 2; + } + return list[middleIndex]; } - const middleIndex = Math.floor(list.length / 2); - const median = list.splice(middleIndex, 1)[0]; - return median; -} } -let nums = [1, 2, 3]; -console.log(calculateMedian(nums)); module.exports = calculateMedian; From 84b5c1ecc457fd5d8945cf36f0480b872e00fdcb Mon Sep 17 00:00:00 2001 From: russom Date: Sat, 18 Jul 2026 22:08:29 +0100 Subject: [PATCH 04/29] .houseNumber added in order to the house number to be printed. --- 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 940a6af83..36d2f865d 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[0]}`); +console.log(`My house number is ${address.houseNumber}`); From 51f62a2852ac8661d37339c6895aaf450770d5ff Mon Sep 17 00:00:00 2001 From: russom Date: Sun, 19 Jul 2026 22:46:29 +0100 Subject: [PATCH 05/29] Author object for of loop changed into for in tp print al the values of the object. --- Sprint-2/debug/author.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/Sprint-2/debug/author.js b/Sprint-2/debug/author.js index 8c2125977..419780a78 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 value in author) { + console.log(author[value]); } + +// The for of loop is trying to iterate through the object like an array. +// For in loop would work better. \ No newline at end of file From ae0b90055ac807f71a3e7833843ed665d62a0a68 Mon Sep 17 00:00:00 2001 From: russom Date: Sun, 19 Jul 2026 23:02:04 +0100 Subject: [PATCH 06/29] Recipe object literal fixed so it can prints the ingredients on new lines. --- Sprint-2/debug/recipe.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/Sprint-2/debug/recipe.js b/Sprint-2/debug/recipe.js index 6cbdd22cd..f4ae9ed86 100644 --- a/Sprint-2/debug/recipe.js +++ b/Sprint-2/debug/recipe.js @@ -11,5 +11,11 @@ const recipe = { }; console.log(`${recipe.title} serves ${recipe.serves} - ingredients: -${recipe}`); +ingredients: +${recipe.ingredients[0]} +${recipe.ingredients[1]} +${recipe.ingredients[2]} +${recipe.ingredients[3]} +${recipe.ingredients[4]}`); + +// On line 15 we can add recipe.ingredients[] From 702b5badd16ab48282db08a03c1a2d5d9fd5959f Mon Sep 17 00:00:00 2001 From: russom Date: Thu, 23 Jul 2026 23:42:39 +0100 Subject: [PATCH 07/29] A fifth element removed from ingredients array which was unneeded --- Sprint-2/debug/recipe.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Sprint-2/debug/recipe.js b/Sprint-2/debug/recipe.js index f4ae9ed86..c84f86c58 100644 --- a/Sprint-2/debug/recipe.js +++ b/Sprint-2/debug/recipe.js @@ -15,7 +15,6 @@ ingredients: ${recipe.ingredients[0]} ${recipe.ingredients[1]} ${recipe.ingredients[2]} -${recipe.ingredients[3]} -${recipe.ingredients[4]}`); +${recipe.ingredients[3]}`); // On line 15 we can add recipe.ingredients[] From 583bb2cfbab224c86525c4d5c99d592df44f0efe Mon Sep 17 00:00:00 2001 From: russom Date: Fri, 24 Jul 2026 13:08:01 +0100 Subject: [PATCH 08/29] Tests created for all cases. --- Sprint-2/implement/contains.test.js | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/Sprint-2/implement/contains.test.js b/Sprint-2/implement/contains.test.js index 326bdb1f2..8303cc021 100644 --- a/Sprint-2/implement/contains.test.js +++ b/Sprint-2/implement/contains.test.js @@ -16,20 +16,37 @@ 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("contains on an object with property returns true", function () { + expect(contains({ a: 1, b: 2 }, "a")).toBe(true); +}); +test("contains on an object without property returns false", function () { + expect(contains({ a: 1, b: 2 }, "d")).toBe(false); +}); // 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", function () { + 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("contain on object with properties", function () { + expect(contains({ c: 4, d: 5 }, "c")).toBe(true); +}); // Given an object with properties // When passed to contains with a non-existent property name // Then it should return false +test("contains on object with non-existent property name returns false", function () { + expect(contains({ c: 4, d: 5 }, "x")).toBe(false); +}); // Given invalid parameters like an array // When passed to contains // Then it should return false or throw an error +test("contain on invalid parameters returns false", function () { + expect(contains([], "0")).toBe(false); +}); From c0f9787ba7c06be1ab16dcc0d25bbbf16190611e Mon Sep 17 00:00:00 2001 From: russom Date: Fri, 24 Jul 2026 13:08:49 +0100 Subject: [PATCH 09/29] function implemented for checking property exist in an object. --- Sprint-2/implement/contains.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/Sprint-2/implement/contains.js b/Sprint-2/implement/contains.js index cd779308a..d2b26ee30 100644 --- a/Sprint-2/implement/contains.js +++ b/Sprint-2/implement/contains.js @@ -1,3 +1,10 @@ -function contains() {} +function contains(object, property) { + for (let key in object) { + if (key === property) { + return true; + } + } + return false; +} module.exports = contains; From 552c68c03668bea472f050f3104846c620c7d63f Mon Sep 17 00:00:00 2001 From: russom Date: Fri, 24 Jul 2026 13:53:50 +0100 Subject: [PATCH 10/29] Initial test case created --- Sprint-2/implement/lookup.test.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Sprint-2/implement/lookup.test.js b/Sprint-2/implement/lookup.test.js index 547e06c5a..179be88fe 100644 --- a/Sprint-2/implement/lookup.test.js +++ b/Sprint-2/implement/lookup.test.js @@ -1,6 +1,12 @@ 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", function() { + const countryCurrency = [ + ["US", "USD"], + ["CA", "CAD"], + ]; + expect(createLookup(countryCurrency)).toEqual({"US": "USD", "CAN": "CAD"}) +}); /* From 9058a376f5a17478b79816b3e38333af1cb5303f Mon Sep 17 00:00:00 2001 From: russom Date: Fri, 24 Jul 2026 13:54:30 +0100 Subject: [PATCH 11/29] Function implemented for creating an abject from key-value array. --- Sprint-2/implement/lookup.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/Sprint-2/implement/lookup.js b/Sprint-2/implement/lookup.js index a6746e07f..a606d30f0 100644 --- a/Sprint-2/implement/lookup.js +++ b/Sprint-2/implement/lookup.js @@ -1,5 +1,8 @@ -function createLookup() { - // implementation here +function createLookup(pairs) { + let obj = {}; + for (let pair of pairs) { + obj[pair[0]] = pair[1] + } } module.exports = createLookup; From ce06e182f5c72bf04d9f84d31a1931ed2ec91f22 Mon Sep 17 00:00:00 2001 From: russom Date: Fri, 24 Jul 2026 14:08:23 +0100 Subject: [PATCH 12/29] Spelling mistakes and bug in CA key fixed. Test passes. --- Sprint-2/implement/lookup.test.js | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/Sprint-2/implement/lookup.test.js b/Sprint-2/implement/lookup.test.js index 179be88fe..d39768202 100644 --- a/Sprint-2/implement/lookup.test.js +++ b/Sprint-2/implement/lookup.test.js @@ -1,11 +1,16 @@ const createLookup = require("./lookup.js"); -test.todo("creates a country currency code lookup for multiple codes", function() { - const countryCurrency = [ +test("creates a country currency code lookup for multiple codes", function() { + /*const countryCurrency = [ ["US", "USD"], ["CA", "CAD"], - ]; - expect(createLookup(countryCurrency)).toEqual({"US": "USD", "CAN": "CAD"}) + ];*/ + expect( + createLookup([ + ["US", "USD"], + ["CA", "CAD"], + ]) + ).toEqual({US: "USD", CA: "CAD",}); }); /* From e6267acfe756d4e0186034bf93ec657875c564af Mon Sep 17 00:00:00 2001 From: russom Date: Fri, 24 Jul 2026 14:08:51 +0100 Subject: [PATCH 13/29] Missing return obj added. --- Sprint-2/implement/lookup.js | 1 + 1 file changed, 1 insertion(+) diff --git a/Sprint-2/implement/lookup.js b/Sprint-2/implement/lookup.js index a606d30f0..2ff7eb12c 100644 --- a/Sprint-2/implement/lookup.js +++ b/Sprint-2/implement/lookup.js @@ -3,6 +3,7 @@ function createLookup(pairs) { for (let pair of pairs) { obj[pair[0]] = pair[1] } + return obj; } module.exports = createLookup; From 14c848cae7c98de1646ebf8612ec63a9900b4612 Mon Sep 17 00:00:00 2001 From: russom Date: Fri, 24 Jul 2026 14:09:53 +0100 Subject: [PATCH 14/29] Commented unneeded code deleted. --- .gitignore | 1 + Sprint-2/implement/lookup.test.js | 4 ---- prep/example2.js | 10 ++++++++++ prep/meadian1.test.js | 7 +++++++ prep/meadian2.js | 22 ++++++++++++++++++++++ prep/mean1.js | 1 + prep/mean1.test.js | 10 ++++++++++ prep/median1.js | 0 prep/newarray.js | 7 +++++++ prep/query-string-test.js | 12 ++++++++++++ 10 files changed, 70 insertions(+), 4 deletions(-) create mode 100644 prep/example2.js create mode 100644 prep/meadian1.test.js create mode 100644 prep/meadian2.js create mode 100644 prep/mean1.js create mode 100644 prep/mean1.test.js create mode 100644 prep/median1.js create mode 100644 prep/newarray.js create mode 100644 prep/query-string-test.js diff --git a/.gitignore b/.gitignore index 8ee70353f..6fca67473 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ node_modules **/.DS_Store .idea package-lock.json +prep/example.js diff --git a/Sprint-2/implement/lookup.test.js b/Sprint-2/implement/lookup.test.js index d39768202..5c1c46e29 100644 --- a/Sprint-2/implement/lookup.test.js +++ b/Sprint-2/implement/lookup.test.js @@ -1,10 +1,6 @@ const createLookup = require("./lookup.js"); test("creates a country currency code lookup for multiple codes", function() { - /*const countryCurrency = [ - ["US", "USD"], - ["CA", "CAD"], - ];*/ expect( createLookup([ ["US", "USD"], diff --git a/prep/example2.js b/prep/example2.js new file mode 100644 index 000000000..c91d5567c --- /dev/null +++ b/prep/example2.js @@ -0,0 +1,10 @@ +function swapFirstArrayLast(arr) { + let swapped = arr[0]; + arr[0] = arr[4]; + arr[4] = swapped; +} + +const myArray = [5, 2, 3, 4, 1]; +swapFirstArrayLast(myArray); +console.log(myArray); +// bracket nottation for setting an array with index diff --git a/prep/meadian1.test.js b/prep/meadian1.test.js new file mode 100644 index 000000000..dc5456112 --- /dev/null +++ b/prep/meadian1.test.js @@ -0,0 +1,7 @@ +test("calculates the median of a list of odd length", () => { + const list = [10, 20, 30, 50, 60]; + const currentOutput = calculateMedian(list); + const targetOutput = 30; + + expect(currentOutput).toEqual(targetOutput); +}); diff --git a/prep/meadian2.js b/prep/meadian2.js new file mode 100644 index 000000000..1161d9a6a --- /dev/null +++ b/prep/meadian2.js @@ -0,0 +1,22 @@ +function calculateMedian(list) { + const middleIndex = Math.floor(list.length / 2); + const median = list.splice(middleIndex, 1)[0]; + + return median; +} +const lists = [1, 2, 3]; +console.log(calculateMedian(lists)); + +/* for (let i = 0; i < list.length; i++) { + if (typeof list[i] !== "number" && typeof list[i] === " ") { + // List is checked whether it is numbers and strings. + return null; // Null is returned if list is not numbers or has mixed values(numbers and strings) + } + const middleIndex = Math.floor(list.length / 2); + + if (list.length % 2 === 0) { + return (list[middleIndex - 1] + list[middleIndex]) / 2; + } + return list[middleIndex]; + } +} */ \ No newline at end of file diff --git a/prep/mean1.js b/prep/mean1.js new file mode 100644 index 000000000..b8ced42b8 --- /dev/null +++ b/prep/mean1.js @@ -0,0 +1 @@ +//A mean function to created for the tests in mean.test.js file. \ No newline at end of file diff --git a/prep/mean1.test.js b/prep/mean1.test.js new file mode 100644 index 000000000..ab7e8557d --- /dev/null +++ b/prep/mean1.test.js @@ -0,0 +1,10 @@ +// Calculating a mean function to be created on mean.js +// test cases to be written on this file prior to creating the mean function. + +test("calculates the mean of a list of numbers", () => { + const list = [3, 50, 7]; + const currentOutput = calculateMean(list); + const targetOutput = 20; + + expect(currentOutput).toEqual(targetOutput); // 20 is (3 + 50 + 7) / 3 +}); \ No newline at end of file diff --git a/prep/median1.js b/prep/median1.js new file mode 100644 index 000000000..e69de29bb diff --git a/prep/newarray.js b/prep/newarray.js new file mode 100644 index 000000000..5bb06b1d2 --- /dev/null +++ b/prep/newarray.js @@ -0,0 +1,7 @@ +function createArray(number) { + const newArray = []; + for (let counter = 1; counter <= number; counter++) { + newArray.push(counter); + } + return newArray; +} diff --git a/prep/query-string-test.js b/prep/query-string-test.js new file mode 100644 index 000000000..0effbff2c --- /dev/null +++ b/prep/query-string-test.js @@ -0,0 +1,12 @@ +//https://example.com/widgets?colour=blue&sort=newest; + +// { colour: blue, sort: "newest"} + +describe("", () => { + test("it will return empty object when we call parseQueryString", () => { + const input = " "; + const currentOutput = parseQueryString(input); + const targetOutput = {}; + expect(currentOutput).toBe(targetOutput); + }); +}); From c66861dcbd3965f0215b1533d4c2b171fd211629 Mon Sep 17 00:00:00 2001 From: russom Date: Sat, 25 Jul 2026 17:52:38 +0100 Subject: [PATCH 15/29] First test values containing = tested --- Sprint-2/implement/querystring.test.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Sprint-2/implement/querystring.test.js b/Sprint-2/implement/querystring.test.js index 328b8df61..3f3cb5e52 100644 --- a/Sprint-2/implement/querystring.test.js +++ b/Sprint-2/implement/querystring.test.js @@ -3,7 +3,7 @@ // Below are some test cases the implementation doesn't handle well. // Fix the implementation for these tests, 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("should parse values containing '='", () => { expect(parseQueryString("equation=a=b-2")).toEqual({ @@ -17,7 +17,7 @@ test("should ignore empty key-value pairs", () => { key2: "value2", }); }); - +/* test("should accept empty string as key or as value", () => { expect(parseQueryString("=value")).toEqual({ "": "value" }); expect(parseQueryString("key")).toEqual({ key: "" }); @@ -46,3 +46,4 @@ test("should store values of a key in an array when the key has 2 or more values foo: "bar", }); }); +*/ From c4dec19a2fcbcaa62ea40f36574cf6967b5bff7e Mon Sep 17 00:00:00 2001 From: russom Date: Sat, 25 Jul 2026 17:53:19 +0100 Subject: [PATCH 16/29] the for loop for testing values containing = fixed --- Sprint-2/implement/querystring.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/Sprint-2/implement/querystring.js b/Sprint-2/implement/querystring.js index 45ec4e5f3..aac4ff264 100644 --- a/Sprint-2/implement/querystring.js +++ b/Sprint-2/implement/querystring.js @@ -6,8 +6,15 @@ function parseQueryString(queryString) { const keyValuePairs = queryString.split("&"); for (const pair of keyValuePairs) { - const [key, value] = pair.split("="); + const everything = pair.split("="); + const key = everything.shift(); + const value = everything.join("="); queryParams[key] = value; + + //console.log(key); + //console.log(values); + //const [key, value] = pair.split("="); + //queryParams[key] = values; } return queryParams; From d5c28aa2686825b607ea170085bccaabbdf36e7d Mon Sep 17 00:00:00 2001 From: russom Date: Sat, 25 Jul 2026 18:25:08 +0100 Subject: [PATCH 17/29] If !pair added for checking empty strings. --- Sprint-2/implement/querystring.js | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/Sprint-2/implement/querystring.js b/Sprint-2/implement/querystring.js index aac4ff264..d32396756 100644 --- a/Sprint-2/implement/querystring.js +++ b/Sprint-2/implement/querystring.js @@ -6,15 +6,13 @@ function parseQueryString(queryString) { const keyValuePairs = queryString.split("&"); for (const pair of keyValuePairs) { + if (!pair) { + continue; + } const everything = pair.split("="); const key = everything.shift(); const value = everything.join("="); queryParams[key] = value; - - //console.log(key); - //console.log(values); - //const [key, value] = pair.split("="); - //queryParams[key] = values; } return queryParams; From b10058d0c0ae090a94b7b19934a7e09f44a4875e Mon Sep 17 00:00:00 2001 From: russom Date: Sat, 25 Jul 2026 20:22:35 +0100 Subject: [PATCH 18/29] decodeURLComponent function used to decode percent-encoded characters test --- Sprint-2/implement/querystring.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Sprint-2/implement/querystring.js b/Sprint-2/implement/querystring.js index d32396756..aa4aa33e0 100644 --- a/Sprint-2/implement/querystring.js +++ b/Sprint-2/implement/querystring.js @@ -7,11 +7,11 @@ function parseQueryString(queryString) { for (const pair of keyValuePairs) { if (!pair) { - continue; + continue; } const everything = pair.split("="); - const key = everything.shift(); - const value = everything.join("="); + const key = decodeURIComponent(everything.shift()); + const value = decodeURIComponent(everything.join("=")); queryParams[key] = value; } From 7fc4886e9d306ac8fd04f81a552a012d1f86238c Mon Sep 17 00:00:00 2001 From: russom Date: Sat, 25 Jul 2026 20:35:00 +0100 Subject: [PATCH 19/29] Using a regular expression .replace(/\+/g, " ") the + sign is removed from the key and value. --- Sprint-2/implement/querystring.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Sprint-2/implement/querystring.js b/Sprint-2/implement/querystring.js index aa4aa33e0..6cbbd6c95 100644 --- a/Sprint-2/implement/querystring.js +++ b/Sprint-2/implement/querystring.js @@ -10,8 +10,8 @@ function parseQueryString(queryString) { continue; } const everything = pair.split("="); - const key = decodeURIComponent(everything.shift()); - const value = decodeURIComponent(everything.join("=")); + const key = decodeURIComponent(everything.shift().replace(/\+/g, " ")); + const value = decodeURIComponent(everything.join("=").replace(/\+/g, " ")); queryParams[key] = value; } From 946461b6667137ac41e47fedac417e4b74b2521b Mon Sep 17 00:00:00 2001 From: russom Date: Sat, 25 Jul 2026 21:24:46 +0100 Subject: [PATCH 20/29] Last test commented out --- Sprint-2/implement/querystring.test.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Sprint-2/implement/querystring.test.js b/Sprint-2/implement/querystring.test.js index 3f3cb5e52..11e8df52b 100644 --- a/Sprint-2/implement/querystring.test.js +++ b/Sprint-2/implement/querystring.test.js @@ -17,7 +17,7 @@ test("should ignore empty key-value pairs", () => { key2: "value2", }); }); -/* + test("should accept empty string as key or as value", () => { expect(parseQueryString("=value")).toEqual({ "": "value" }); expect(parseQueryString("key")).toEqual({ key: "" }); @@ -40,10 +40,11 @@ test("should replace '+' by ' '", () => { // Stretch exercise: Handling query strings that contain identical keys // Delete this test if you are not working on this optional case +/* test("should store values of a key in an array when the key has 2 or more values", () => { expect(parseQueryString("key=value1&key=value2&key=value3&foo=bar")).toEqual({ key: ["value1", "value2", "value3"], foo: "bar", }); }); -*/ +*/ \ No newline at end of file From 37e70cba3c08631752f44c0339bfd6b9a25f0cea Mon Sep 17 00:00:00 2001 From: russom Date: Sat, 25 Jul 2026 21:37:45 +0100 Subject: [PATCH 21/29] Test created for all the cases --- Sprint-2/implement/tally.test.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/Sprint-2/implement/tally.test.js b/Sprint-2/implement/tally.test.js index 2ceffa8dd..74c370c9c 100644 --- a/Sprint-2/implement/tally.test.js +++ b/Sprint-2/implement/tally.test.js @@ -19,16 +19,27 @@ 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("tally on an array returns an object with item", function() { + expect(tally(["a"])).etEqual({a: 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", function() { + expect(tally([])).toEqual({}) +}); // Given an array with duplicate items // When passed to tally // Then it should return counts for each unique item +test("tally on duplicate items returns count of items", function() { + expect(tally(["a", "a", "a"])).toEqual({a: 3}) +}) // Given an invalid input like a string // When passed to tally // Then it should throw an error +test("tally on invalid input throws an error", function() { + expect(tally("string")).toBe("error") +}) \ No newline at end of file From 1b4063ad58f69f0766e0ca5b6da8c4e2ba5c0beb Mon Sep 17 00:00:00 2001 From: russom Date: Sat, 25 Jul 2026 21:56:23 +0100 Subject: [PATCH 22/29] function implemented for checking duplicate items. --- Sprint-2/implement/tally.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Sprint-2/implement/tally.js b/Sprint-2/implement/tally.js index f47321812..8041a9c59 100644 --- a/Sprint-2/implement/tally.js +++ b/Sprint-2/implement/tally.js @@ -1,3 +1,9 @@ -function tally() {} +function tally(arr) { + const count = {}; + for (const item of arr) { + count[item] = (count[item] || 0) + 1; + } + return count; +} module.exports = tally; From af59161ff60edf0390712c200536597c11b11723 Mon Sep 17 00:00:00 2001 From: russom Date: Sat, 25 Jul 2026 22:15:56 +0100 Subject: [PATCH 23/29] for the last test for invalid input test adjusted so jest can check first and catch the error. --- Sprint-2/implement/tally.test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Sprint-2/implement/tally.test.js b/Sprint-2/implement/tally.test.js index 74c370c9c..c5a0029fd 100644 --- a/Sprint-2/implement/tally.test.js +++ b/Sprint-2/implement/tally.test.js @@ -20,7 +20,7 @@ const tally = require("./tally.js"); // When passed an array of items // Then it should return an object containing the count for each unique item test("tally on an array returns an object with item", function() { - expect(tally(["a"])).etEqual({a: 1}) + expect(tally(["a"])).toEqual({a: 1}) }) // Given an empty array @@ -41,5 +41,5 @@ test("tally on duplicate items returns count of items", function() { // When passed to tally // Then it should throw an error test("tally on invalid input throws an error", function() { - expect(tally("string")).toBe("error") + expect(() => tally("string")).toThrow("Invalid input") }) \ No newline at end of file From 0877d2d13438f5de7eec2f74fda410315bfe330e Mon Sep 17 00:00:00 2001 From: russom Date: Sat, 25 Jul 2026 22:17:27 +0100 Subject: [PATCH 24/29] if statement added to check the item is number not string. --- Sprint-2/implement/tally.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Sprint-2/implement/tally.js b/Sprint-2/implement/tally.js index 8041a9c59..04969f933 100644 --- a/Sprint-2/implement/tally.js +++ b/Sprint-2/implement/tally.js @@ -1,4 +1,7 @@ function tally(arr) { + if (!Array.isArray(arr)) { + throw new Error("Invalid input"); + } const count = {}; for (const item of arr) { count[item] = (count[item] || 0) + 1; From 4c0bba52a302e7f3d2ae2eb2f006cb4801f6c560 Mon Sep 17 00:00:00 2001 From: russom Date: Fri, 31 Jul 2026 22:24:18 +0100 Subject: [PATCH 25/29] invertedObj value is wrapped in square brackets to access the property inside value --- Sprint-2/interpret/invert.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/Sprint-2/interpret/invert.js b/Sprint-2/interpret/invert.js index bb353fb1f..5c5c7c93b 100644 --- a/Sprint-2/interpret/invert.js +++ b/Sprint-2/interpret/invert.js @@ -10,20 +10,28 @@ 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 } +// - Ony 1 is printed // b) What is the current return value when invert is called with { a: 1, b: 2 } +// - 1 and 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() returns an array of a given object's own enumerable string keyed property key-value pair // d) Explain why the current return value is different from the target output +// - invertedObj only referenced the swapped value to key. We should have another variable to swap and reference key to value // e) Fix the implementation of invert (and write tests to prove it's fixed!) From 5518e5e3fe53a702089e2b309ea9959fb35b2620 Mon Sep 17 00:00:00 2001 From: russom Date: Fri, 31 Jul 2026 22:25:00 +0100 Subject: [PATCH 26/29] test created to check invert function works as intended --- Sprint-2/interpret/inverts.test.js | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 Sprint-2/interpret/inverts.test.js diff --git a/Sprint-2/interpret/inverts.test.js b/Sprint-2/interpret/inverts.test.js new file mode 100644 index 000000000..bf5e850f1 --- /dev/null +++ b/Sprint-2/interpret/inverts.test.js @@ -0,0 +1,10 @@ +const invert = require("./invert.js"); + +test("invert on an object with key-value returns key-value swapped", function () { + expect(invert({ x: 10, y: 20 })).toEqual({ 10: "x", 20: "y" }); + expect(invert({ a: 1, b: 2 })).toEqual({ 1: "a", 2: "b" }); + expect(invert({ distance1: 105, distance2: 201 })) + .toEqual({ 105: "distance1", 201: "distance2" }); + expect(invert({ fruit: "apple", veg: "carrot" })) + .toEqual({ "apple": "fruit", "carrot": "veg" }); +}); From ed6927a41c559739fc708226cb47cdfc4eb751e1 Mon Sep 17 00:00:00 2001 From: russom Date: Fri, 31 Jul 2026 22:50:12 +0100 Subject: [PATCH 27/29] Restore file to match upstream main --- Sprint-1/fix/median.js | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index 561e70090..b22590bc6 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -6,18 +6,9 @@ // or 'list' has mixed values (the function is expected to sort only numbers). function calculateMedian(list) { - for (let i = 0; i < list.length; i++) { - if (typeof list[i] === "number" && typeof list[i] === " ") { - // List is checked whether it is numbers and strings. - return null; // Null is returned if list is not numbers or has mixed values(numbers and strings) - } - const middleIndex = Math.floor(list.length / 2); - - if (list.length % 2 === 0) { - return (list[middleIndex - 1] + list[middleIndex]) / 2; - } - return list[middleIndex]; - } + const middleIndex = Math.floor(list.length / 2); + const median = list.splice(middleIndex, 1)[0]; + return median; } module.exports = calculateMedian; From d43853588ce36be8c51db2952883a72c5be7af63 Mon Sep 17 00:00:00 2001 From: russom Date: Fri, 31 Jul 2026 23:01:45 +0100 Subject: [PATCH 28/29] Remove prep files and restore files to match upstream main --- .gitignore | 1 - 1 file changed, 1 deletion(-) diff --git a/.gitignore b/.gitignore index 6fca67473..8ee70353f 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,3 @@ node_modules **/.DS_Store .idea package-lock.json -prep/example.js From 3163a9c633ce0279aa9435dea1a97bc864a57b1f Mon Sep 17 00:00:00 2001 From: russom Date: Fri, 31 Jul 2026 23:04:23 +0100 Subject: [PATCH 29/29] Remove prep files from tracking --- prep/example2.js | 10 ---------- prep/meadian1.test.js | 7 ------- prep/meadian2.js | 22 ---------------------- prep/mean1.js | 1 - prep/mean1.test.js | 10 ---------- prep/median1.js | 0 prep/newarray.js | 7 ------- prep/query-string-test.js | 12 ------------ 8 files changed, 69 deletions(-) delete mode 100644 prep/example2.js delete mode 100644 prep/meadian1.test.js delete mode 100644 prep/meadian2.js delete mode 100644 prep/mean1.js delete mode 100644 prep/mean1.test.js delete mode 100644 prep/median1.js delete mode 100644 prep/newarray.js delete mode 100644 prep/query-string-test.js diff --git a/prep/example2.js b/prep/example2.js deleted file mode 100644 index c91d5567c..000000000 --- a/prep/example2.js +++ /dev/null @@ -1,10 +0,0 @@ -function swapFirstArrayLast(arr) { - let swapped = arr[0]; - arr[0] = arr[4]; - arr[4] = swapped; -} - -const myArray = [5, 2, 3, 4, 1]; -swapFirstArrayLast(myArray); -console.log(myArray); -// bracket nottation for setting an array with index diff --git a/prep/meadian1.test.js b/prep/meadian1.test.js deleted file mode 100644 index dc5456112..000000000 --- a/prep/meadian1.test.js +++ /dev/null @@ -1,7 +0,0 @@ -test("calculates the median of a list of odd length", () => { - const list = [10, 20, 30, 50, 60]; - const currentOutput = calculateMedian(list); - const targetOutput = 30; - - expect(currentOutput).toEqual(targetOutput); -}); diff --git a/prep/meadian2.js b/prep/meadian2.js deleted file mode 100644 index 1161d9a6a..000000000 --- a/prep/meadian2.js +++ /dev/null @@ -1,22 +0,0 @@ -function calculateMedian(list) { - const middleIndex = Math.floor(list.length / 2); - const median = list.splice(middleIndex, 1)[0]; - - return median; -} -const lists = [1, 2, 3]; -console.log(calculateMedian(lists)); - -/* for (let i = 0; i < list.length; i++) { - if (typeof list[i] !== "number" && typeof list[i] === " ") { - // List is checked whether it is numbers and strings. - return null; // Null is returned if list is not numbers or has mixed values(numbers and strings) - } - const middleIndex = Math.floor(list.length / 2); - - if (list.length % 2 === 0) { - return (list[middleIndex - 1] + list[middleIndex]) / 2; - } - return list[middleIndex]; - } -} */ \ No newline at end of file diff --git a/prep/mean1.js b/prep/mean1.js deleted file mode 100644 index b8ced42b8..000000000 --- a/prep/mean1.js +++ /dev/null @@ -1 +0,0 @@ -//A mean function to created for the tests in mean.test.js file. \ No newline at end of file diff --git a/prep/mean1.test.js b/prep/mean1.test.js deleted file mode 100644 index ab7e8557d..000000000 --- a/prep/mean1.test.js +++ /dev/null @@ -1,10 +0,0 @@ -// Calculating a mean function to be created on mean.js -// test cases to be written on this file prior to creating the mean function. - -test("calculates the mean of a list of numbers", () => { - const list = [3, 50, 7]; - const currentOutput = calculateMean(list); - const targetOutput = 20; - - expect(currentOutput).toEqual(targetOutput); // 20 is (3 + 50 + 7) / 3 -}); \ No newline at end of file diff --git a/prep/median1.js b/prep/median1.js deleted file mode 100644 index e69de29bb..000000000 diff --git a/prep/newarray.js b/prep/newarray.js deleted file mode 100644 index 5bb06b1d2..000000000 --- a/prep/newarray.js +++ /dev/null @@ -1,7 +0,0 @@ -function createArray(number) { - const newArray = []; - for (let counter = 1; counter <= number; counter++) { - newArray.push(counter); - } - return newArray; -} diff --git a/prep/query-string-test.js b/prep/query-string-test.js deleted file mode 100644 index 0effbff2c..000000000 --- a/prep/query-string-test.js +++ /dev/null @@ -1,12 +0,0 @@ -//https://example.com/widgets?colour=blue&sort=newest; - -// { colour: blue, sort: "newest"} - -describe("", () => { - test("it will return empty object when we call parseQueryString", () => { - const input = " "; - const currentOutput = parseQueryString(input); - const targetOutput = {}; - expect(currentOutput).toBe(targetOutput); - }); -});