From 65dcedf8ee3bfaa9a59ce40a1f95251a5c29ee68 Mon Sep 17 00:00:00 2001 From: Diana <40689028+ausiejute@users.noreply.github.com> Date: Sun, 8 Nov 2026 15:31:59 +0000 Subject: [PATCH 01/13] none --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index 8ee70353fa..d62df10be6 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,5 @@ node_modules **/.DS_Store .idea package-lock.json +mean.test.js +mean.js From cefdbcca3df54792acb7d131c96b8c7f4a468a6d Mon Sep 17 00:00:00 2001 From: Diana <40689028+ausiejute@users.noreply.github.com> Date: Sun, 8 Nov 2026 22:31:11 +0000 Subject: [PATCH 02/13] Debug 3 exercises --- Sprint-2/debug/address.js | 2 +- Sprint-2/debug/author.js | 5 +++-- Sprint-2/debug/recipe.js | 5 +++-- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/Sprint-2/debug/address.js b/Sprint-2/debug/address.js index 940a6af832..36d2f865db 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}`); diff --git a/Sprint-2/debug/author.js b/Sprint-2/debug/author.js index 8c2125977d..b0cfc8d159 100644 --- a/Sprint-2/debug/author.js +++ b/Sprint-2/debug/author.js @@ -1,4 +1,5 @@ // Predict and explain first... +// it won't loop over it as if it was an array // 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 +12,6 @@ const author = { alive: true, }; -for (const value of author) { - console.log(value); +for (let prop in author) { + console.log(author[prop]); } diff --git a/Sprint-2/debug/recipe.js b/Sprint-2/debug/recipe.js index 6cbdd22cdc..20d1a84ed2 100644 --- a/Sprint-2/debug/recipe.js +++ b/Sprint-2/debug/recipe.js @@ -1,4 +1,5 @@ // Predict and explain first... +// it will not print ingredients at all, as the values aren't accessed // This program should log out the title, how many it serves and the ingredients. // Each ingredient should be logged on a new line @@ -11,5 +12,5 @@ const recipe = { }; console.log(`${recipe.title} serves ${recipe.serves} - ingredients: -${recipe}`); +ingredients: +${recipe.ingredients.join("\n")}`); From d9c82d9b317bc9b706203cd33ce8f6e07f28b8d4 Mon Sep 17 00:00:00 2001 From: Diana <40689028+ausiejute@users.noreply.github.com> Date: Mon, 16 Nov 2026 23:02:41 +0000 Subject: [PATCH 03/13] Write tests and implementation of the Contain exercise --- Sprint-2/implement/contains.js | 8 ++++++- Sprint-2/implement/contains.test.js | 37 ++++++++++++++++++++++++++++- 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/Sprint-2/implement/contains.js b/Sprint-2/implement/contains.js index cd779308a0..6e0ee30074 100644 --- a/Sprint-2/implement/contains.js +++ b/Sprint-2/implement/contains.js @@ -1,3 +1,9 @@ -function contains() {} +function contains(obj, key) { + if (obj === null || typeof obj !== "object" || Array.isArray(obj)) { + throw new Error("contains requires an object"); + } + + return Object.hasOwn(obj, key); +} module.exports = contains; diff --git a/Sprint-2/implement/contains.test.js b/Sprint-2/implement/contains.test.js index 719272787d..f266046931 100644 --- a/Sprint-2/implement/contains.test.js +++ b/Sprint-2/implement/contains.test.js @@ -23,18 +23,53 @@ as an array isn't an object // Given an empty object // When passed to contains // Then it should return false -test.todo("contains on empty object returns false"); +test("should return false given an empty object", () => { + 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("should return true when the object has the property", () => { + expect(contains({ age: 8, birthYear: 2019 }, "age")).toBe(true); +}); // Given an object with properties // When passed to contains with a non-existent property name // Then it should return false +test("should return false given a non-existent property", () => { + expect(contains({ year: 2026, month: "July" }, "a")).toBe(false); +}); // Given a value that isn't an object - an array, a string, a number, // null, or no argument at all // When passed to contains // Then it should throw Error("contains requires an object") // (careful: typeof [] and typeof null are both "object") +test("should throw an error when passed an array", () => { + expect(() => contains([2, 5, 9], "a")).toThrow( + new Error("contains requires an object") + ); +}); + +test("should throw an error when passed a string", () => { + expect(() => + contains("sun", "a").toThrow(new Error("contains requires an object")) + ); +}); + +test("should throw an error when passed a number", () => { + expect(() => + contains(9, "a").toThrow(new Error("contains requires an object")) + ); +}); + +test("should throw an error when passed a null", () => { + expect(() => + contains("sun", null).toThrow(new Error("contains requires an object")) + ); +}); + +test("should throw an error when passed no argument", () => { + expect(() => contains().toThrow(new Error("contains requires an object"))); +}); From d627dd1684e9a5147c4f24ea23ff485a01cda6b2 Mon Sep 17 00:00:00 2001 From: Diana <40689028+ausiejute@users.noreply.github.com> Date: Mon, 16 Nov 2026 23:38:46 +0000 Subject: [PATCH 04/13] Write the implementation of Lookup function --- Sprint-2/implement/lookup.js | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/Sprint-2/implement/lookup.js b/Sprint-2/implement/lookup.js index a6746e07f5..729973052f 100644 --- a/Sprint-2/implement/lookup.js +++ b/Sprint-2/implement/lookup.js @@ -1,5 +1,12 @@ -function createLookup() { - // implementation here -} +function createLookup(countryCurrencyPairs) { + const lookup = {}; + + countryCurrencyPairs.forEach(function (pair) { + const country = pair[0]; + const currency = pair[1]; + lookup[country] = currency; + }); + return lookup; +} module.exports = createLookup; From f6ccd3500785f942637eecad3a8244119133ac5b Mon Sep 17 00:00:00 2001 From: Diana <40689028+ausiejute@users.noreply.github.com> Date: Mon, 16 Nov 2026 23:39:23 +0000 Subject: [PATCH 05/13] Write the test for a Lookup function --- Sprint-2/implement/lookup.test.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/Sprint-2/implement/lookup.test.js b/Sprint-2/implement/lookup.test.js index 547e06c5a7..9e3b4a4f9e 100644 --- a/Sprint-2/implement/lookup.test.js +++ b/Sprint-2/implement/lookup.test.js @@ -1,6 +1,13 @@ 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", () => { + expect( + createLookup([ + ["US", "USD"], + ["CA", "CAD"], + ]) + ).toEqual({ US: "USD", CA: "CAD" }); +}); /* From ef14433cbfd86aa7a4301a8b63c1e80b73231763 Mon Sep 17 00:00:00 2001 From: Diana <40689028+ausiejute@users.noreply.github.com> Date: Tue, 17 Nov 2026 13:17:07 +0000 Subject: [PATCH 06/13] Write required tests for Tally function --- Sprint-2/implement/tally.test.js | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/Sprint-2/implement/tally.test.js b/Sprint-2/implement/tally.test.js index ca763c2965..d080fb38a3 100644 --- a/Sprint-2/implement/tally.test.js +++ b/Sprint-2/implement/tally.test.js @@ -23,12 +23,32 @@ 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("tally returns counts for each unique item", () => { + expect(tally(["o", "o", "l", "m", "m", "m"])).toEqual({ o: 2, l: 1, m: 3 }); +}); // Given an invalid input like a string, a number, or no argument at all // When passed to tally // Then it should throw Error("tally requires an array") +test("tally throws an error if passed a string", () => { +expect(() => tally('nothing')).toThrow( + new Error("tally requires an object") +); +}) + +test("tally throws an error if passed a number", () => { +expect(() => tally(7)).toThrow( + new Error("tally requires an object") +); + +test("tally throws an error if not passed anything", () => { +expect(() => tally().toThrow( + new Error("tally requires an object") +); \ No newline at end of file From 0b31435db7353c695aec64ed20f60e2ff7cb98cb Mon Sep 17 00:00:00 2001 From: Diana <40689028+ausiejute@users.noreply.github.com> Date: Tue, 17 Nov 2026 13:47:02 +0000 Subject: [PATCH 07/13] Fix Tally test formatting --- Sprint-2/implement/tally.test.js | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/Sprint-2/implement/tally.test.js b/Sprint-2/implement/tally.test.js index d080fb38a3..9529e31a25 100644 --- a/Sprint-2/implement/tally.test.js +++ b/Sprint-2/implement/tally.test.js @@ -19,6 +19,9 @@ 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 returns an object containing the count for each unique item", () => { + expect(tally(["a", "b", "c"])).toEqual({ a: 1, b: 1, c: 1 }); +}); // Given an empty array // When passed to tally @@ -38,17 +41,13 @@ test("tally returns counts for each unique item", () => { // When passed to tally // Then it should throw Error("tally requires an array") test("tally throws an error if passed a string", () => { -expect(() => tally('nothing')).toThrow( - new Error("tally requires an object") -); -}) + expect(() => tally("nothing")).toThrow(new Error("tally requires an array")); +}); test("tally throws an error if passed a number", () => { -expect(() => tally(7)).toThrow( - new Error("tally requires an object") -); + expect(() => tally(7)).toThrow(new Error("tally requires an array")); +}); test("tally throws an error if not passed anything", () => { -expect(() => tally().toThrow( - new Error("tally requires an object") -); \ No newline at end of file + expect(() => tally()).toThrow(new Error("tally requires an array")); +}); From 65b2a62376de20c94a28c7d75ad28cfcae4f98e0 Mon Sep 17 00:00:00 2001 From: Diana <40689028+ausiejute@users.noreply.github.com> Date: Tue, 17 Nov 2026 13:47:23 +0000 Subject: [PATCH 08/13] Implement Tally function --- Sprint-2/implement/tally.js | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/Sprint-2/implement/tally.js b/Sprint-2/implement/tally.js index f47321812e..e5dbcbcbf5 100644 --- a/Sprint-2/implement/tally.js +++ b/Sprint-2/implement/tally.js @@ -1,3 +1,18 @@ -function tally() {} +function tally(items) { + if (!Array.isArray(items)) { + throw new Error("tally requires an array"); + } + + const counts = {}; + + for (const item of items) { + if (Object.hasOwn(counts, item)) { + counts[item] += 1; + } else { + counts[item] = 1; + } + } + return counts; +} module.exports = tally; From 79d9c90307d97131e5757734d992b170ae7a42cc Mon Sep 17 00:00:00 2001 From: Diana <40689028+ausiejute@users.noreply.github.com> Date: Tue, 17 Nov 2026 15:07:02 +0000 Subject: [PATCH 09/13] Fix the implementation of he parseQueryString function to match the tests --- Sprint-2/implement/querystring.js | 15 +++++++++++++-- Sprint-2/implement/querystring.test.js | 12 +----------- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/Sprint-2/implement/querystring.js b/Sprint-2/implement/querystring.js index 45ec4e5f37..4bbd27850a 100644 --- a/Sprint-2/implement/querystring.js +++ b/Sprint-2/implement/querystring.js @@ -3,11 +3,22 @@ function parseQueryString(queryString) { if (queryString.length === 0) { return queryParams; } + const keyValuePairs = queryString.split("&"); for (const pair of keyValuePairs) { - const [key, value] = pair.split("="); - queryParams[key] = value; + if (pair === "") { + continue; + } + + const parts = pair.split("="); + const key = parts[0]; + const value = parts.slice(1).join("="); + + const decodeKey = decodeURIComponent(key.replace(/\+/g, " ")); + const decodeValue = decodeURIComponent(value.replace(/\+/g, " ")); + + queryParams[decodeKey] = decodeValue; } return queryParams; diff --git a/Sprint-2/implement/querystring.test.js b/Sprint-2/implement/querystring.test.js index 328b8df617..a2f400262c 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({ @@ -36,13 +36,3 @@ test("should replace '+' by ' '", () => { "full name": "John Doe", }); }); - -// 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", - }); -}); From e5fd18106e4bce086654d254543b0dc20429f68b Mon Sep 17 00:00:00 2001 From: Diana <40689028+ausiejute@users.noreply.github.com> Date: Tue, 17 Nov 2026 15:20:07 +0000 Subject: [PATCH 10/13] Write an extra test and adjust the implementation accordingly --- Sprint-2/implement/querystring.js | 5 +++++ Sprint-2/implement/querystring.test.js | 4 ++++ 2 files changed, 9 insertions(+) diff --git a/Sprint-2/implement/querystring.js b/Sprint-2/implement/querystring.js index 4bbd27850a..e884d1d088 100644 --- a/Sprint-2/implement/querystring.js +++ b/Sprint-2/implement/querystring.js @@ -1,5 +1,10 @@ function parseQueryString(queryString) { const queryParams = {}; + + if (queryString.startsWith("?")) { + queryString = queryString.slice(1); + } + if (queryString.length === 0) { return queryParams; } diff --git a/Sprint-2/implement/querystring.test.js b/Sprint-2/implement/querystring.test.js index a2f400262c..0d8a3598ad 100644 --- a/Sprint-2/implement/querystring.test.js +++ b/Sprint-2/implement/querystring.test.js @@ -36,3 +36,7 @@ test("should replace '+' by ' '", () => { "full name": "John Doe", }); }); + +test("should accept a leading question mark", () => { + expect(parseQueryString("?name=Sam")).toEqual({ name: "Sam" }); +}); From 4297e40d98167d2f10f23a734d9d4faf83287e7e Mon Sep 17 00:00:00 2001 From: Diana <40689028+ausiejute@users.noreply.github.com> Date: Tue, 17 Nov 2026 17:13:08 +0000 Subject: [PATCH 11/13] Answer the questions and fix the Invert function --- Sprint-2/interpret/invert.js | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/Sprint-2/interpret/invert.js b/Sprint-2/interpret/invert.js index bb353fb1fd..1206d6dc13 100644 --- a/Sprint-2/interpret/invert.js +++ b/Sprint-2/interpret/invert.js @@ -10,20 +10,22 @@ 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? - +// it returns the object's properties as pairs where each of them contains a key and it's value. +// It's needed here to make key and value pairs accessible (to eventually build an inverted version of the object). // d) Explain why the current return value is different from the target output - +// In invertedObj.key = value the dot means 'use the property literary named 'key'', not the variable key. +// So each loop pass overwrites that same property and the returned object contains only the last value, like {key: 'blue}. // e) Fix the implementation of invert (and write tests to prove it's fixed!) From d701c04d4836e5ae8633548bd753325517423b73 Mon Sep 17 00:00:00 2001 From: Diana <40689028+ausiejute@users.noreply.github.com> Date: Wed, 18 Nov 2026 09:26:24 +0000 Subject: [PATCH 12/13] Create a test file and write tests for the Invert function --- Sprint-2/interpret/invert.js | 2 +- Sprint-2/interpret/invert.test.js | 12 ++++++++++++ 2 files changed, 13 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 1206d6dc13..0f3444568a 100644 --- a/Sprint-2/interpret/invert.js +++ b/Sprint-2/interpret/invert.js @@ -15,7 +15,7 @@ function invert(obj) { return invertedObj; } - +module.exports = invert; // 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 } diff --git a/Sprint-2/interpret/invert.test.js b/Sprint-2/interpret/invert.test.js new file mode 100644 index 0000000000..c45de9fabc --- /dev/null +++ b/Sprint-2/interpret/invert.test.js @@ -0,0 +1,12 @@ +const invert = require("./invert.js"); + +test("invert should swap each key and value when passed an object", () => { + expect(invert({ a: "cherry", b: "kiwi" })).toEqual({ + cherry: "a", + kiwi: "b", + }); +}); + +test("invert should return an empty object when passed an empty object", () => { + expect(invert({})).toEqual({}); +}); From 47eb14706119ef026d346fdbbb425194e0e67e41 Mon Sep 17 00:00:00 2001 From: Diana <40689028+ausiejute@users.noreply.github.com> Date: Wed, 18 Nov 2026 13:55:09 +0000 Subject: [PATCH 13/13] Fix Contains error-handling tests --- Sprint-2/implement/contains.test.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/Sprint-2/implement/contains.test.js b/Sprint-2/implement/contains.test.js index f266046931..27bf6abf93 100644 --- a/Sprint-2/implement/contains.test.js +++ b/Sprint-2/implement/contains.test.js @@ -53,23 +53,23 @@ test("should throw an error when passed an array", () => { }); test("should throw an error when passed a string", () => { - expect(() => - contains("sun", "a").toThrow(new Error("contains requires an object")) + expect(() => contains("sun", "a")).toThrow( + new Error("contains requires an object") ); }); test("should throw an error when passed a number", () => { - expect(() => - contains(9, "a").toThrow(new Error("contains requires an object")) + expect(() => contains(9, "a")).toThrow( + new Error("contains requires an object") ); }); test("should throw an error when passed a null", () => { - expect(() => - contains("sun", null).toThrow(new Error("contains requires an object")) + expect(() => contains(null, "a")).toThrow( + new Error("contains requires an object") ); }); test("should throw an error when passed no argument", () => { - expect(() => contains().toThrow(new Error("contains requires an object"))); + expect(() => contains()).toThrow(new Error("contains requires an object")); });