From 03bb1b12b59cc6c2de2a7b97ef16470f1bad3821 Mon Sep 17 00:00:00 2001 From: Ben Solar Date: Sun, 29 Mar 2026 00:49:31 +0100 Subject: [PATCH 1/5] Add prep folder to the main branch --- Prep/mean.js | 20 ++++++++++++++++++++ Prep/mean.test.js | 9 +++++++++ Prep/median.js | 6 ++++++ Prep/median.test.js | 0 Prep/summation.js | 21 +++++++++++++++++++++ Prep/summation.test.js | 9 +++++++++ 6 files changed, 65 insertions(+) create mode 100644 Prep/mean.js create mode 100644 Prep/mean.test.js create mode 100644 Prep/median.js create mode 100644 Prep/median.test.js create mode 100644 Prep/summation.js create mode 100644 Prep/summation.test.js diff --git a/Prep/mean.js b/Prep/mean.js new file mode 100644 index 000000000..57e562338 --- /dev/null +++ b/Prep/mean.js @@ -0,0 +1,20 @@ +// function calculateMean(list) { +// const total = list[0] + list[1] + list[2]; +// const mean = total / list.length; + +// return mean; +// } + +// Running the same code with a loop function inside the calculateMean function. + +function calculateMean(list) { + let total = 0; + let i = 0; + + for (let i = 0; i < list.length; i++) { + total = total + list[i]; + } + return total / list.length; +} + +module.exports = calculateMean; diff --git a/Prep/mean.test.js b/Prep/mean.test.js new file mode 100644 index 000000000..d515c1dff --- /dev/null +++ b/Prep/mean.test.js @@ -0,0 +1,9 @@ +const calculateMean = require("./mean"); + +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 +}); diff --git a/Prep/median.js b/Prep/median.js new file mode 100644 index 000000000..80a27fa99 --- /dev/null +++ b/Prep/median.js @@ -0,0 +1,6 @@ +function calculateMedian(list) { + const middleIndex = Math.floor(list.length / 2); + const median = list.splice(middleIndex, 1)[0]; + + return median; +} diff --git a/Prep/median.test.js b/Prep/median.test.js new file mode 100644 index 000000000..e69de29bb diff --git a/Prep/summation.js b/Prep/summation.js new file mode 100644 index 000000000..ee4897184 --- /dev/null +++ b/Prep/summation.js @@ -0,0 +1,21 @@ +// function sumValues(list) { +// let total = 0; +// total += list[0]; // access a list element and add to total +// total += list[1]; +// total += list[2]; +// total += list[3]; +// total += list[4]; +// return total; +// } + +// sumValues([1, 2, 3, 4, 5]); + +function sumValues(list) { + let total = 0; + for (i = 0; i < list.length; i++) { + total = total += list[i]; + } + return total; +} + +module.exports = sumValues; diff --git a/Prep/summation.test.js b/Prep/summation.test.js new file mode 100644 index 000000000..4724ce58e --- /dev/null +++ b/Prep/summation.test.js @@ -0,0 +1,9 @@ +const sumValues = require("./summation"); + +test("calculates the mean of a list of numbers", () => { + const list = [3, 50, 7]; + const currentOutput = sumValues(list); + const targetOutput = 60; + + expect(currentOutput).toEqual(targetOutput); +}); From a9413d075e797670093f7b1c856e829e1f13696c Mon Sep 17 00:00:00 2001 From: Ben Solar Date: Sun, 29 Mar 2026 00:51:59 +0100 Subject: [PATCH 2/5] Remove Prep folder from branch coursework/sprint-2 --- Prep/mean.js | 20 -------------------- Prep/mean.test.js | 9 --------- Prep/median.js | 6 ------ Prep/median.test.js | 0 Prep/summation.js | 21 --------------------- Prep/summation.test.js | 9 --------- 6 files changed, 65 deletions(-) delete mode 100644 Prep/mean.js delete mode 100644 Prep/mean.test.js delete mode 100644 Prep/median.js delete mode 100644 Prep/median.test.js delete mode 100644 Prep/summation.js delete mode 100644 Prep/summation.test.js diff --git a/Prep/mean.js b/Prep/mean.js deleted file mode 100644 index 57e562338..000000000 --- a/Prep/mean.js +++ /dev/null @@ -1,20 +0,0 @@ -// function calculateMean(list) { -// const total = list[0] + list[1] + list[2]; -// const mean = total / list.length; - -// return mean; -// } - -// Running the same code with a loop function inside the calculateMean function. - -function calculateMean(list) { - let total = 0; - let i = 0; - - for (let i = 0; i < list.length; i++) { - total = total + list[i]; - } - return total / list.length; -} - -module.exports = calculateMean; diff --git a/Prep/mean.test.js b/Prep/mean.test.js deleted file mode 100644 index d515c1dff..000000000 --- a/Prep/mean.test.js +++ /dev/null @@ -1,9 +0,0 @@ -const calculateMean = require("./mean"); - -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 -}); diff --git a/Prep/median.js b/Prep/median.js deleted file mode 100644 index 80a27fa99..000000000 --- a/Prep/median.js +++ /dev/null @@ -1,6 +0,0 @@ -function calculateMedian(list) { - const middleIndex = Math.floor(list.length / 2); - const median = list.splice(middleIndex, 1)[0]; - - return median; -} diff --git a/Prep/median.test.js b/Prep/median.test.js deleted file mode 100644 index e69de29bb..000000000 diff --git a/Prep/summation.js b/Prep/summation.js deleted file mode 100644 index ee4897184..000000000 --- a/Prep/summation.js +++ /dev/null @@ -1,21 +0,0 @@ -// function sumValues(list) { -// let total = 0; -// total += list[0]; // access a list element and add to total -// total += list[1]; -// total += list[2]; -// total += list[3]; -// total += list[4]; -// return total; -// } - -// sumValues([1, 2, 3, 4, 5]); - -function sumValues(list) { - let total = 0; - for (i = 0; i < list.length; i++) { - total = total += list[i]; - } - return total; -} - -module.exports = sumValues; diff --git a/Prep/summation.test.js b/Prep/summation.test.js deleted file mode 100644 index 4724ce58e..000000000 --- a/Prep/summation.test.js +++ /dev/null @@ -1,9 +0,0 @@ -const sumValues = require("./summation"); - -test("calculates the mean of a list of numbers", () => { - const list = [3, 50, 7]; - const currentOutput = sumValues(list); - const targetOutput = 60; - - expect(currentOutput).toEqual(targetOutput); -}); From 2b25dc927676fd3910a2269ddf3765b89324ed3f Mon Sep 17 00:00:00 2001 From: Ben Solar Date: Thu, 9 Apr 2026 13:43:21 +0200 Subject: [PATCH 3/5] Fix Refactor 3 files in Sprint-2/debug folder. --- Sprint-2/debug/address.js | 7 ++++++- Sprint-2/debug/author.js | 8 +++++++- Sprint-2/debug/recipe.js | 10 +++++++++- 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/Sprint-2/debug/address.js b/Sprint-2/debug/address.js index 940a6af83..9a130e510 100644 --- a/Sprint-2/debug/address.js +++ b/Sprint-2/debug/address.js @@ -4,6 +4,11 @@ // but it isn't working... // Fix anything that isn't working +/* +When we test the file without fixing we get "My house number is [object Object]" +We need to properly export the house number: adress.houseNumber. +*/ + const address = { houseNumber: 42, street: "Imaginary Road", @@ -12,4 +17,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..abbe421f5 100644 --- a/Sprint-2/debug/author.js +++ b/Sprint-2/debug/author.js @@ -1,7 +1,12 @@ // Predict and explain first... +/* +TypeError: author is not iterable +The way the code is written is that author is an 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 +*/ const author = { firstName: "Zadie", @@ -11,6 +16,7 @@ const author = { alive: true, }; -for (const value of author) { +for (const value of Object.values(author)) { + // gives you an array of all the values console.log(value); } diff --git a/Sprint-2/debug/recipe.js b/Sprint-2/debug/recipe.js index 6cbdd22cd..fc2523a47 100644 --- a/Sprint-2/debug/recipe.js +++ b/Sprint-2/debug/recipe.js @@ -1,5 +1,13 @@ // Predict and explain first... +/* +First of all the output will not give us the proper ingredients. We will again get [object Object]. +We can fix this by using the correct array object recipe.ingredients. + +Secondly we don't loop through the ingredients and create a new line \n after each ingredient. + +*/ + // 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 +20,4 @@ const recipe = { console.log(`${recipe.title} serves ${recipe.serves} ingredients: -${recipe}`); +${recipe.ingredients.join("\n")}`); //You can do this by joining the array with \n (newline) From aaa1675d6992c84c5df55c80b9c9f55de83419fa Mon Sep 17 00:00:00 2001 From: Ben Solar Date: Thu, 9 Apr 2026 13:44:25 +0200 Subject: [PATCH 4/5] Fix all exercises from Sprint-2/implement folder. All tests are passing, functions are created to pass all the tests properly. --- Sprint-2/implement/contains.js | 24 +++++++++- Sprint-2/implement/contains.test.js | 15 ++++++- Sprint-2/implement/lookup.js | 18 +++++++- Sprint-2/implement/lookup.test.js | 22 +++++++++- Sprint-2/implement/querystring.js | 10 ++++- Sprint-2/implement/querystring.test.js | 61 ++++++++++++++++++++++++-- Sprint-2/implement/tally.js | 25 ++++++++++- Sprint-2/implement/tally.test.js | 28 +++++++++++- 8 files changed, 190 insertions(+), 13 deletions(-) diff --git a/Sprint-2/implement/contains.js b/Sprint-2/implement/contains.js index cd779308a..f64e04615 100644 --- a/Sprint-2/implement/contains.js +++ b/Sprint-2/implement/contains.js @@ -1,3 +1,25 @@ -function contains() {} +function contains(objectToCheck, propertyName) { + if (Array.isArray(objectToCheck)) { + throw new Error("Invalid parameter, No arrays allowed"); + } + if (typeof objectToCheck !== "object" || objectToCheck === null) { + return false; + } + return propertyName in objectToCheck; +} module.exports = contains; + +/* +If the first argument is an array + Throw an error: "Invalid parameter, No arrays allowed" + +If the first argument is not an object + Return false + +If the object has the property (second argument) + Return true + +Otherwise + Return false +*/ diff --git a/Sprint-2/implement/contains.test.js b/Sprint-2/implement/contains.test.js index 326bdb1f2..fc80af968 100644 --- a/Sprint-2/implement/contains.test.js +++ b/Sprint-2/implement/contains.test.js @@ -20,16 +20,29 @@ 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", () => { + 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("Given an object with properties that are included, return true", () => { + expect(contains({ a: 1, b: 2 }, "a")).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 properties that are NON included, return false", () => { + expect(contains({ a: 1, b: 2 }, "c")).toBe(false); +}); // Given invalid parameters like an array // When passed to contains // Then it should return false or throw an error +test("Given an object with invalid properties such as arrays, trow error", () => { + expect(() => contains([1, 2, 3])).toThrow( + "Invalid parameter, No arrays allowed" + ); +}); diff --git a/Sprint-2/implement/lookup.js b/Sprint-2/implement/lookup.js index a6746e07f..a56f1ee16 100644 --- a/Sprint-2/implement/lookup.js +++ b/Sprint-2/implement/lookup.js @@ -1,5 +1,19 @@ -function createLookup() { - // implementation here +function createLookup(countryCurrencyPairs) { + return Object.fromEntries(countryCurrencyPairs); } module.exports = createLookup; + +/* +The Object.fromEntries() static method transforms a list of key-value pairs into an object. + +const entries = new Map([ + ["foo", "bar"], + ["baz", 42], +]); + +const obj = Object.fromEntries(entries); + +console.log(obj); +// Expected output: Object { foo: "bar", baz: 42 } +*/ diff --git a/Sprint-2/implement/lookup.test.js b/Sprint-2/implement/lookup.test.js index 547e06c5a..754a2af7b 100644 --- a/Sprint-2/implement/lookup.test.js +++ b/Sprint-2/implement/lookup.test.js @@ -1,7 +1,25 @@ const createLookup = require("./lookup.js"); -test.todo("creates a country currency code lookup for multiple codes"); - +test("Given an array of code pairs, create a lookup object representing country code", () => { + expect(createLookup([["US", "USD"]])).toEqual({ US: "USD" }); +}); + +test("Given an array of code pairs CA - CAD, create a lookup object representing country code", () => { + expect(createLookup([["CA", "CAD"]])).toEqual({ CA: "CAD" }); +}); + +test("Given 2 arrays of code pairs, create a lookup object with 2 object pairs", () => { + expect( + createLookup([ + ["US", "USD"], + ["CA", "CAD"], + ]) + ).toEqual({ US: "USD", CA: "CAD" }); +}); + +test("Given an empty array of code pairs, we should return an empty object", () => { + expect(createLookup([])).toEqual({}); +}); /* Create a lookup object of key value pairs from an array of code pairs diff --git a/Sprint-2/implement/querystring.js b/Sprint-2/implement/querystring.js index 45ec4e5f3..da86793b8 100644 --- a/Sprint-2/implement/querystring.js +++ b/Sprint-2/implement/querystring.js @@ -6,8 +6,14 @@ function parseQueryString(queryString) { const keyValuePairs = queryString.split("&"); for (const pair of keyValuePairs) { - const [key, value] = pair.split("="); - queryParams[key] = value; + const whereIs = pair.indexOf("="); + if (whereIs === -1) { + queryParams[pair] = ""; + } else { + const key = pair.slice(0, whereIs); + const value = pair.slice(whereIs + 1); + queryParams[key] = value; + } } return queryParams; diff --git a/Sprint-2/implement/querystring.test.js b/Sprint-2/implement/querystring.test.js index 3e218b789..ae3b9f56d 100644 --- a/Sprint-2/implement/querystring.test.js +++ b/Sprint-2/implement/querystring.test.js @@ -3,10 +3,65 @@ // Below is one test case for an edge case the implementation doesn't handle well. // Fix the implementation for this test, 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("parses querystring values containing =", () => { +test("parses a value that contains =", () => { expect(parseQueryString("equation=x=y+1")).toEqual({ - "equation": "x=y+1", + equation: "x=y+1", + }); +}); + +// Empty string edge case: +test("returns an empty object for an empty string", () => { + expect(parseQueryString("")).toEqual({}); +}); + +// Key with empty value: +test("parses a key with an empty value", () => { + expect(parseQueryString("Key=")).toEqual({ Key: "" }); +}); + +// Value with an empty key: +test("parses a value with an empty key", () => { + expect(parseQueryString("=Value")).toEqual({ "": "Value" }); +}); + +// When there's no = in the Querystring: +test("parses a query string without = as an empty value", () => { + expect(parseQueryString("nothingToSeeHere")).toEqual({ + nothingToSeeHere: "", + }); +}); + +// Multiple pairs per Querystring: +test("parses multiple key-value pairs", () => { + expect(parseQueryString("name=Ben&city=Amsterdam")).toEqual({ + name: "Ben", + city: "Amsterdam", + }); +}); + +// Multiple pairs with missing values: +test("parses multiple pairs when one value is missing", () => { + expect(parseQueryString("name=Ben&city=&country=Netherlands")).toEqual({ + name: "Ben", + city: "", + country: "Netherlands", + }); +}); + +// Pairs with a missing key: +test("parses multiple pairs when one key is missing", () => { + expect(parseQueryString("name=Ben&=Amsterdam")).toEqual({ + name: "Ben", + "": "Amsterdam", + }); +}); + +// Multiple pairs where one value contains =: +test("parses multiple pairs when one value contains =", () => { + expect(parseQueryString("equation=x=y+1&answer=42")).toEqual({ + equation: "x=y+1", + answer: "42", }); }); diff --git a/Sprint-2/implement/tally.js b/Sprint-2/implement/tally.js index f47321812..0477e1bbc 100644 --- a/Sprint-2/implement/tally.js +++ b/Sprint-2/implement/tally.js @@ -1,3 +1,26 @@ -function tally() {} +function tally(arrayItems) { + if (!Array.isArray(arrayItems)) { + throw new Error("The input is not an Array"); + } + // Createa an empty object to store the counted value appears in the array. + const counter = {}; + for (const item of arrayItems) { + if (counter[item]) { + counter[item] += 1; + } else { + counter[item] = 1; + } + } + return counter; +} module.exports = tally; + +/* +If the input is not an array, throw an error. +Make an empty object called counts. +For each item in the array: +If the item is already in counts, add 1 to its value. +If the item is not in counts, set its value to 1. +Return the counts object. +*/ diff --git a/Sprint-2/implement/tally.test.js b/Sprint-2/implement/tally.test.js index 2ceffa8dd..7a0a8931d 100644 --- a/Sprint-2/implement/tally.test.js +++ b/Sprint-2/implement/tally.test.js @@ -23,12 +23,38 @@ 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("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 duplicate items, return counts for each unique item", () => { + expect(tally(["a", "a", "b", "b", "c"])).toEqual({ a: 2, b: 2, c: 1 }); +}); + +// Test with 1 value in the array +test("Given an array of duplicate items, return counts for each unique item", () => { + expect(tally(["a"])).toEqual({ a: 1 }); +}); + // Given an invalid input like a string // When passed to tally // Then it should throw an error + +test("Given an invalid input like string, trow error: The input is not an Array", () => { + expect(() => tally("Malakia not a string")).toThrow( + "The input is not an Array" + ); +}); + +/* +Remember: “toThrow needs a function, toEqual needs a value. +If you want to test for an error, always wrap the function call in another function. + +expect(() => myFunc(badInput)).toThrow(); + +*/ From 12faccd64876109a63ae63f81be2ed1c470c062a Mon Sep 17 00:00:00 2001 From: Ben Solar Date: Thu, 9 Apr 2026 16:37:21 +0200 Subject: [PATCH 5/5] Fix error in Sprint-2/interpret folder so it swaps keys and values correctly. Now uses bracket notation to make the value the new property name, not just the word "key" --- Sprint-2/interpret/invert.js | 19 ++++++++++++++++++- Sprint-2/interpret/invert.test.js | 29 +++++++++++++++++++++++++++++ 2 files changed, 47 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..8dc55abfa 100644 --- a/Sprint-2/interpret/invert.js +++ b/Sprint-2/interpret/invert.js @@ -10,20 +10,37 @@ function invert(obj) { const invertedObj = {}; for (const [key, value] of Object.entries(obj)) { - invertedObj.key = value; + // Use the old value as the "new" key, and the "old" key as the new value. + // invertedObj.key = value; => This always creates a property called "key" not the value of "key" + invertedObj[value] = key; } return invertedObj; } // a) What is the current return value when invert is called with { a : 1 } +// Current return value: { key: 1 } +// It used the word `key` as the property name. // b) What is the current return value when invert is called with { a: 1, b: 2 } +// Current return value: { key: 2 } +// The second loop overwrites the first one. // c) What is the target return value when invert is called with {a : 1, b: 2} +// Target return value: { 1: "a", 2: "b" } +// The old values become keys, and the old keys become values. // c) What does Object.entries return? Why is it needed in this program? +// `Object.entries(obj)` returns an array of key-value pairs. +// Example: `Object.entries({ a: 1, b: 2 })` becomes `[["a", 1], ["b", 2]]`. +// We use it to get both the key and value while looping. +// MDN: Object.entries() +// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/entries // d) Explain why the current return value is different from the target output +// `invertedObj.key` always makes a property called `key`. +// We need `invertedObj[value]` so JavaScript uses the real value as the property name. // e) Fix the implementation of invert (and write tests to prove it's fixed!) + +module.exports = invert; diff --git a/Sprint-2/interpret/invert.test.js b/Sprint-2/interpret/invert.test.js new file mode 100644 index 000000000..4197069b4 --- /dev/null +++ b/Sprint-2/interpret/invert.test.js @@ -0,0 +1,29 @@ +const invert = require("./invert.js"); + +// When the object has one key-value pair: +test("swaps the key and value in an object with one property", () => { + expect(invert({ a: 1 })).toEqual({ + 1: "a", + }); +}); + +// When the object has multiple key-value pairs: +test("swaps all keys and values in an object with multiple properties", () => { + expect(invert({ a: 1, b: 2 })).toEqual({ + 1: "a", + 2: "b", + }); +}); + +// When the object is empty: +test("returns an empty object when given an empty object", () => { + expect(invert({})).toEqual({}); +}); + +// When the values are strings: +test("uses the original string values as keys in the new object", () => { + expect(invert({ first: "Benzaki", second: "BenziMou" })).toEqual({ + Benzaki: "first", + BenziMou: "second", + }); +});