From 5d54c313118eef89d6eebce7dfc1e2269ea15900 Mon Sep 17 00:00:00 2001 From: marscancode Date: Thu, 12 Nov 2026 18:08:17 +0000 Subject: [PATCH 01/32] fix house number access in address object --- 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 8d0e38a479dd49143d2114dac7e82774f7c83d6f Mon Sep 17 00:00:00 2001 From: marscancode Date: Thu, 12 Nov 2026 18:20:11 +0000 Subject: [PATCH 02/32] fixed loop to print author object values --- Sprint-2/debug/author.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sprint-2/debug/author.js b/Sprint-2/debug/author.js index 8c2125977..7ba8f4c75 100644 --- a/Sprint-2/debug/author.js +++ b/Sprint-2/debug/author.js @@ -11,6 +11,6 @@ const author = { alive: true, }; -for (const value of author) { +for (const value of Object.values(author)) { console.log(value); } From 1f4ed4baed3ed51a2ef0b35876923c8877b43c03 Mon Sep 17 00:00:00 2001 From: marscancode Date: Thu, 12 Nov 2026 18:40:51 +0000 Subject: [PATCH 03/32] fixed recipe and loop output --- Sprint-2/debug/recipe.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Sprint-2/debug/recipe.js b/Sprint-2/debug/recipe.js index 6cbdd22cd..533ac4ec5 100644 --- a/Sprint-2/debug/recipe.js +++ b/Sprint-2/debug/recipe.js @@ -11,5 +11,7 @@ const recipe = { }; console.log(`${recipe.title} serves ${recipe.serves} - ingredients: -${recipe}`); + ingredients:`); +for (const ingredient of recipe.ingredients) { + console.log(ingredient); +} From 7faefe4cbc8b2d410ebd3c12babb55c66815d1d5 Mon Sep 17 00:00:00 2001 From: marscancode Date: Sun, 15 Nov 2026 12:41:20 +0000 Subject: [PATCH 04/32] Add empty object test for contains --- Sprint-2/implement/contains.test.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Sprint-2/implement/contains.test.js b/Sprint-2/implement/contains.test.js index 719272787..5f51768c8 100644 --- a/Sprint-2/implement/contains.test.js +++ b/Sprint-2/implement/contains.test.js @@ -23,8 +23,10 @@ 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("returns false when given an empty object", () => { + expect(contains({})).toBe(false); +}); + // Given an object with properties // When passed to contains with an existing property name // Then it should return true From 0c0415dd6d02c8ad8a935d455b0e12bcc5be426a Mon Sep 17 00:00:00 2001 From: marscancode Date: Sun, 15 Nov 2026 13:30:29 +0000 Subject: [PATCH 05/32] implemented check for empty object in contains --- Sprint-2/implement/contains.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/Sprint-2/implement/contains.js b/Sprint-2/implement/contains.js index cd779308a..93798f1e7 100644 --- a/Sprint-2/implement/contains.js +++ b/Sprint-2/implement/contains.js @@ -1,3 +1,6 @@ -function contains() {} - +function contains(obj, propertyName) { + if (Object.keys(obj).length === 0) { + return false; + } +} module.exports = contains; From e0d3618ceb827a509bdb8da3af2bc2ac53159e05 Mon Sep 17 00:00:00 2001 From: marscancode Date: Sun, 15 Nov 2026 13:46:58 +0000 Subject: [PATCH 06/32] added test to check wether object contains property --- Sprint-2/implement/contains.test.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/Sprint-2/implement/contains.test.js b/Sprint-2/implement/contains.test.js index 5f51768c8..c35999840 100644 --- a/Sprint-2/implement/contains.test.js +++ b/Sprint-2/implement/contains.test.js @@ -19,14 +19,17 @@ as an array isn't an object // 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("returns true when given an object and property name that contains property", () => { + expect(contains({ a: 1, b: 2 }, "a")).toBe(true); +}); // Given an empty object // When passed to contains // Then it should return false + test("returns false when given an empty object", () => { expect(contains({})).toBe(false); }); - + // Given an object with properties // When passed to contains with an existing property name // Then it should return true From cd8545ba27c7bf1ba172f285ec7c0e67ec10b24f Mon Sep 17 00:00:00 2001 From: marscancode Date: Sun, 15 Nov 2026 14:06:28 +0000 Subject: [PATCH 07/32] implemented property check in contains --- Sprint-2/implement/contains.js | 1 + 1 file changed, 1 insertion(+) diff --git a/Sprint-2/implement/contains.js b/Sprint-2/implement/contains.js index 93798f1e7..559594c2f 100644 --- a/Sprint-2/implement/contains.js +++ b/Sprint-2/implement/contains.js @@ -2,5 +2,6 @@ function contains(obj, propertyName) { if (Object.keys(obj).length === 0) { return false; } + return Object.keys(obj).includes(propertyName); } module.exports = contains; From 92710cda0cb986be1b5336889ff57ece55d18bd7 Mon Sep 17 00:00:00 2001 From: marscancode Date: Sun, 15 Nov 2026 14:13:40 +0000 Subject: [PATCH 08/32] added missing property test for contains --- Sprint-2/implement/contains.test.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Sprint-2/implement/contains.test.js b/Sprint-2/implement/contains.test.js index c35999840..44ba0461e 100644 --- a/Sprint-2/implement/contains.test.js +++ b/Sprint-2/implement/contains.test.js @@ -22,6 +22,10 @@ as an array isn't an object test("returns true when given an object and property name that contains property", () => { expect(contains({ a: 1, b: 2 }, "a")).toBe(true); }); + +test("returns false when given an object and property name that doesn't contain the property", () => { + expect(contains({ a: 1, b: 2 }, "c")).toBe(false); +}); // Given an empty object // When passed to contains // Then it should return false From 123e14286e6d367485832cb1d18e18ed383b5cbf Mon Sep 17 00:00:00 2001 From: marscancode Date: Sun, 15 Nov 2026 14:37:50 +0000 Subject: [PATCH 09/32] added test to check wether contains throws an error for arrays --- Sprint-2/implement/contains.test.js | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/Sprint-2/implement/contains.test.js b/Sprint-2/implement/contains.test.js index 44ba0461e..6f0a27343 100644 --- a/Sprint-2/implement/contains.test.js +++ b/Sprint-2/implement/contains.test.js @@ -34,16 +34,11 @@ test("returns false when given an empty object", () => { expect(contains({})).toBe(false); }); -// Given an object with properties -// When passed to contains with an existing property name -// Then it should return true - -// Given an object with properties -// When passed to contains with a non-existent property name -// Then it should return 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("throws an error when given an array", () => { + expect(() => contains([1, 2, 3], "a")).toThrow("contains requires an object"); +}); From a77051872e99b1d6ebcd1c52780799d55ae79ac3 Mon Sep 17 00:00:00 2001 From: marscancode Date: Sun, 15 Nov 2026 14:45:20 +0000 Subject: [PATCH 10/32] implemented contains to handle arrays --- Sprint-2/implement/contains.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Sprint-2/implement/contains.js b/Sprint-2/implement/contains.js index 559594c2f..72e48d8c3 100644 --- a/Sprint-2/implement/contains.js +++ b/Sprint-2/implement/contains.js @@ -1,4 +1,7 @@ function contains(obj, propertyName) { + if (Array.isArray(obj)) { + throw new Error("contains requires an object"); + } if (Object.keys(obj).length === 0) { return false; } From 1c0e82dd34af0037722c9e04f465f23a3ba36fef Mon Sep 17 00:00:00 2001 From: marscancode Date: Sun, 15 Nov 2026 14:48:07 +0000 Subject: [PATCH 11/32] added test to check contains throws an error for strings --- Sprint-2/implement/contains.test.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Sprint-2/implement/contains.test.js b/Sprint-2/implement/contains.test.js index 6f0a27343..c7a366aaa 100644 --- a/Sprint-2/implement/contains.test.js +++ b/Sprint-2/implement/contains.test.js @@ -42,3 +42,9 @@ test("returns false when given an empty object", () => { test("throws an error when given an array", () => { expect(() => contains([1, 2, 3], "a")).toThrow("contains requires an object"); }); + +test("throws an error when given a string", () => { + expect(() => contains("hello", "a")).toThrow( + "contains requires an object" + ); +}); From 6a554706d1a04f44a537ee23dd7f4853d417687f Mon Sep 17 00:00:00 2001 From: marscancode Date: Sun, 15 Nov 2026 14:52:30 +0000 Subject: [PATCH 12/32] implemented contains to handle strings --- Sprint-2/implement/contains.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Sprint-2/implement/contains.js b/Sprint-2/implement/contains.js index 72e48d8c3..100b0a1e2 100644 --- a/Sprint-2/implement/contains.js +++ b/Sprint-2/implement/contains.js @@ -2,6 +2,9 @@ function contains(obj, propertyName) { if (Array.isArray(obj)) { throw new Error("contains requires an object"); } + if (typeof obj === "string") { + throw new Error("contains requires an object"); + } if (Object.keys(obj).length === 0) { return false; } From 6befbcd75c2375a8d697e7ccc48a2463f1b2d2c1 Mon Sep 17 00:00:00 2001 From: marscancode Date: Sun, 15 Nov 2026 14:54:46 +0000 Subject: [PATCH 13/32] added test to check wether contains throws an error when given a number --- Sprint-2/implement/contains.test.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Sprint-2/implement/contains.test.js b/Sprint-2/implement/contains.test.js index c7a366aaa..2bd56422f 100644 --- a/Sprint-2/implement/contains.test.js +++ b/Sprint-2/implement/contains.test.js @@ -44,7 +44,8 @@ test("throws an error when given an array", () => { }); test("throws an error when given a string", () => { - expect(() => contains("hello", "a")).toThrow( - "contains requires an object" - ); + expect(() => contains("hello", "a")).toThrow("contains requires an object"); +}); +test("throws an error when given a number", () => { + expect(() => contains(42, "a")).toThrow("contains requires an object"); }); From e9bc9c62082959a855a9ce3bcbbf607e548129be Mon Sep 17 00:00:00 2001 From: marscancode Date: Sun, 15 Nov 2026 14:58:00 +0000 Subject: [PATCH 14/32] implemented contains to handle numbers --- Sprint-2/implement/contains.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Sprint-2/implement/contains.js b/Sprint-2/implement/contains.js index 100b0a1e2..98763efba 100644 --- a/Sprint-2/implement/contains.js +++ b/Sprint-2/implement/contains.js @@ -5,6 +5,9 @@ function contains(obj, propertyName) { if (typeof obj === "string") { throw new Error("contains requires an object"); } + if (typeof obj === "number") { + throw new Error("contains requires an object"); + } if (Object.keys(obj).length === 0) { return false; } From 4866df97574a8b83a90cc6fda22e3d024835b83a Mon Sep 17 00:00:00 2001 From: marscancode Date: Sun, 15 Nov 2026 14:59:50 +0000 Subject: [PATCH 15/32] test to check wether contains throws an error for null --- Sprint-2/implement/contains.test.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Sprint-2/implement/contains.test.js b/Sprint-2/implement/contains.test.js index 2bd56422f..4690eeba7 100644 --- a/Sprint-2/implement/contains.test.js +++ b/Sprint-2/implement/contains.test.js @@ -49,3 +49,6 @@ test("throws an error when given a string", () => { test("throws an error when given a number", () => { expect(() => contains(42, "a")).toThrow("contains requires an object"); }); +test("throws an error when given null", () => { + expect(() => contains(null, "a")).toThrow("contains requires an object"); +}); From 9e574107b3b102da98b2f98e1523132b2d54c588 Mon Sep 17 00:00:00 2001 From: marscancode Date: Sun, 15 Nov 2026 15:03:16 +0000 Subject: [PATCH 16/32] implemented contains to handle null --- Sprint-2/implement/contains.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Sprint-2/implement/contains.js b/Sprint-2/implement/contains.js index 98763efba..e9e746e53 100644 --- a/Sprint-2/implement/contains.js +++ b/Sprint-2/implement/contains.js @@ -8,6 +8,9 @@ function contains(obj, propertyName) { if (typeof obj === "number") { throw new Error("contains requires an object"); } + if (obj === null) { + throw new Error("contains requires an object"); + } if (Object.keys(obj).length === 0) { return false; } From 8d4c1cc1fb9bfaf3624691cc82afae4cb302dcbc Mon Sep 17 00:00:00 2001 From: marscancode Date: Sun, 15 Nov 2026 15:05:30 +0000 Subject: [PATCH 17/32] added to test to check wether contains throws an error with no argument passed --- Sprint-2/implement/contains.test.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Sprint-2/implement/contains.test.js b/Sprint-2/implement/contains.test.js index 4690eeba7..a8cfb2c60 100644 --- a/Sprint-2/implement/contains.test.js +++ b/Sprint-2/implement/contains.test.js @@ -52,3 +52,7 @@ test("throws an error when given a number", () => { test("throws an error when given null", () => { expect(() => contains(null, "a")).toThrow("contains requires an object"); }); + +test("throws an error when given no argument", () => { + expect(() => contains()).toThrow("contains requires an object"); +}); From ab6963fbd7b5ad198f579a71843187762062bb73 Mon Sep 17 00:00:00 2001 From: marscancode Date: Sun, 15 Nov 2026 15:09:34 +0000 Subject: [PATCH 18/32] implemented contains to handle undefined --- Sprint-2/implement/contains.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Sprint-2/implement/contains.js b/Sprint-2/implement/contains.js index e9e746e53..434ad4170 100644 --- a/Sprint-2/implement/contains.js +++ b/Sprint-2/implement/contains.js @@ -11,9 +11,13 @@ function contains(obj, propertyName) { if (obj === null) { throw new Error("contains requires an object"); } + if (obj === undefined) { + throw new Error("contains requires an object"); + } if (Object.keys(obj).length === 0) { return false; } + return Object.keys(obj).includes(propertyName); } module.exports = contains; From 7e908fb22b7a617e34cf9afb5fd85e6b22505284 Mon Sep 17 00:00:00 2001 From: marscancode Date: Sun, 15 Nov 2026 15:35:33 +0000 Subject: [PATCH 19/32] added test for lookup with multiple currency pairs --- Sprint-2/implement/lookup.test.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Sprint-2/implement/lookup.test.js b/Sprint-2/implement/lookup.test.js index 547e06c5a..9360d1dd0 100644 --- a/Sprint-2/implement/lookup.test.js +++ b/Sprint-2/implement/lookup.test.js @@ -2,6 +2,10 @@ const createLookup = require("./lookup.js"); test.todo("creates a country currency code lookup for multiple codes"); +test("given currency code pairs returns an object with the country and its currency code", () => { + expect(createLookup([["JP", "JPY"],["GB","GBP"]])).toEqual({ JP: "JPY", GB: "GBP" }); +}); + /* Create a lookup object of key value pairs from an array of code pairs From aadb7e6a3028c3f2e8176f354e5c62987a323c40 Mon Sep 17 00:00:00 2001 From: marscancode Date: Sun, 15 Nov 2026 15:53:11 +0000 Subject: [PATCH 20/32] update --- Sprint-2/implement/lookup.test.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/Sprint-2/implement/lookup.test.js b/Sprint-2/implement/lookup.test.js index 9360d1dd0..850f9c4e8 100644 --- a/Sprint-2/implement/lookup.test.js +++ b/Sprint-2/implement/lookup.test.js @@ -1,9 +1,12 @@ const createLookup = require("./lookup.js"); -test.todo("creates a country currency code lookup for multiple codes"); - test("given currency code pairs returns an object with the country and its currency code", () => { - expect(createLookup([["JP", "JPY"],["GB","GBP"]])).toEqual({ JP: "JPY", GB: "GBP" }); + expect( + createLookup([ + ["JP", "JPY"], + ["GB", "GBP"], + ]) + ).toEqual({ JP: "JPY", GB: "GBP" }); }); /* From aeef6269f21b8d3c95e8b52a11b3bfa9224cadd4 Mon Sep 17 00:00:00 2001 From: marscancode Date: Sun, 15 Nov 2026 15:54:28 +0000 Subject: [PATCH 21/32] Implemented country currency lookup --- Sprint-2/implement/lookup.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/Sprint-2/implement/lookup.js b/Sprint-2/implement/lookup.js index a6746e07f..1922fa222 100644 --- a/Sprint-2/implement/lookup.js +++ b/Sprint-2/implement/lookup.js @@ -1,5 +1,9 @@ -function createLookup() { - // implementation here +function createLookup(countryCurrencyPairs) { + const lookup = {}; + for (const pair of countryCurrencyPairs) { + lookup[pair[0]] = pair[1]; + } + return lookup; } module.exports = createLookup; From c362f4ea0fdb4667fabff69572057920f87cda24 Mon Sep 17 00:00:00 2001 From: marscancode Date: Sun, 15 Nov 2026 19:44:25 +0000 Subject: [PATCH 22/32] test to check lookup returns an empty object when given an empty array --- Sprint-2/implement/lookup.test.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Sprint-2/implement/lookup.test.js b/Sprint-2/implement/lookup.test.js index 850f9c4e8..00960b654 100644 --- a/Sprint-2/implement/lookup.test.js +++ b/Sprint-2/implement/lookup.test.js @@ -9,6 +9,9 @@ test("given currency code pairs returns an object with the country and its curre ).toEqual({ JP: "JPY", GB: "GBP" }); }); +test("given an empty array returns an empty object", () => { + expect(createLookup([])).toEqual({}); +}); /* Create a lookup object of key value pairs from an array of code pairs From f50f02a55873116ed166291429f8d912cc998ec1 Mon Sep 17 00:00:00 2001 From: marscancode Date: Sun, 15 Nov 2026 19:52:45 +0000 Subject: [PATCH 23/32] added test to check that lookup handles duplicate country codes --- Sprint-2/implement/lookup.test.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/Sprint-2/implement/lookup.test.js b/Sprint-2/implement/lookup.test.js index 00960b654..8eff97710 100644 --- a/Sprint-2/implement/lookup.test.js +++ b/Sprint-2/implement/lookup.test.js @@ -12,6 +12,15 @@ test("given currency code pairs returns an object with the country and its curre test("given an empty array returns an empty object", () => { expect(createLookup([])).toEqual({}); }); + +test("reassigns the previous currency when a country code is duplicated", () => { + expect( + createLookup([ + ["GB", "GBP"], + ["GB", "XXX"], + ]) + ).toEqual({ GB: "XXX" }); +}); /* Create a lookup object of key value pairs from an array of code pairs From 9731b4dfa166860ea16d40b4aa81292d00f3ba78 Mon Sep 17 00:00:00 2001 From: marscancode Date: Sun, 15 Nov 2026 21:08:49 +0000 Subject: [PATCH 24/32] Removed optional stretch test for duplicate keys --- Sprint-2/implement/querystring.test.js | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/Sprint-2/implement/querystring.test.js b/Sprint-2/implement/querystring.test.js index 328b8df61..a2f400262 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 f7ffc4d77f4a4b49a79218dc1bddecb894b17cfc Mon Sep 17 00:00:00 2001 From: marscancode Date: Sun, 15 Nov 2026 21:09:43 +0000 Subject: [PATCH 25/32] Fixed query string handling for edge cases --- Sprint-2/implement/querystring.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/Sprint-2/implement/querystring.js b/Sprint-2/implement/querystring.js index 45ec4e5f3..01a49839d 100644 --- a/Sprint-2/implement/querystring.js +++ b/Sprint-2/implement/querystring.js @@ -3,10 +3,14 @@ function parseQueryString(queryString) { if (queryString.length === 0) { return queryParams; } - const keyValuePairs = queryString.split("&"); + const keyValuePairs = queryString.split("&").filter((pair) => pair !== ""); for (const pair of keyValuePairs) { - const [key, value] = pair.split("="); + const parts = pair.split("="); + const key = decodeURIComponent(parts[0].replace(/\+/g, " ")); + const value = decodeURIComponent( + parts.slice(1).join("=").replace(/\+/g, " ") + ); queryParams[key] = value; } From 7a823ce6ece85a5f26001af6b48fb565184eca48 Mon Sep 17 00:00:00 2001 From: marscancode Date: Sun, 15 Nov 2026 21:16:41 +0000 Subject: [PATCH 26/32] added test for tally with an empty array --- Sprint-2/implement/tally.test.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Sprint-2/implement/tally.test.js b/Sprint-2/implement/tally.test.js index ca763c296..55a4c77ad 100644 --- a/Sprint-2/implement/tally.test.js +++ b/Sprint-2/implement/tally.test.js @@ -23,7 +23,9 @@ 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 From 21d99721b1def37d5de72203b1c5f8823dd65c43 Mon Sep 17 00:00:00 2001 From: marscancode Date: Mon, 16 Nov 2026 08:53:18 +0000 Subject: [PATCH 27/32] Test to check if tally counts items in an array --- Sprint-2/implement/tally.js | 19 ++++++++++++++++++- Sprint-2/implement/tally.test.js | 4 +++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/Sprint-2/implement/tally.js b/Sprint-2/implement/tally.js index f47321812..1065e5bbf 100644 --- a/Sprint-2/implement/tally.js +++ b/Sprint-2/implement/tally.js @@ -1,3 +1,20 @@ -function tally() {} +function tally(items) { + const counts = {}; + if (!Array.isArray(items)) { + throw new Error("tally requires an array"); + } + + for (let i = 0; i < items.length; i++) { + const item = items[i]; + + if (Object.keys(counts).includes(item)) { + counts[item] = counts[item] + 1; + } else { + counts[item] = 1; + } + } + + return counts; +} module.exports = tally; diff --git a/Sprint-2/implement/tally.test.js b/Sprint-2/implement/tally.test.js index 55a4c77ad..80fdef889 100644 --- a/Sprint-2/implement/tally.test.js +++ b/Sprint-2/implement/tally.test.js @@ -19,7 +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("given an array of items, returns an object containing the count for each item", () => { + expect(tally(["a", "a", "b", "c"])).toEqual({ a: 2, b: 1, c: 1 }); +}); // Given an empty array // When passed to tally // Then it should return an empty object From 590c736d6b4d3ad13f61c513a7145ec4a1280db3 Mon Sep 17 00:00:00 2001 From: marscancode Date: Mon, 16 Nov 2026 09:04:03 +0000 Subject: [PATCH 28/32] Test to check if tally rejects invalid inputs --- Sprint-2/implement/tally.test.js | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/Sprint-2/implement/tally.test.js b/Sprint-2/implement/tally.test.js index 80fdef889..002fd253d 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 +// 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, returns an object containing the count for each item", () => { expect(tally(["a", "a", "b", "c"])).toEqual({ a: 2, b: 1, c: 1 }); }); @@ -29,10 +32,17 @@ 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 - // 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("given invalid input like a string, throw an error message", () => { + expect(() => tally("hello")).toThrow("tally requires an array"); +}); + +test("given invalid input like a number, throw an error message", () => { + expect(() => tally(7)).toThrow("tally requires an array"); +}); + +test("given invalid input like no argument, throw an error message", () => { + expect(() => tally()).toThrow("tally requires an array"); +}); From a5f091f51afb54bff0fe59fc468d7977177ac9c6 Mon Sep 17 00:00:00 2001 From: marscancode Date: Mon, 16 Nov 2026 09:52:30 +0000 Subject: [PATCH 29/32] invert exercise answers --- Sprint-2/interpret/invert.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/Sprint-2/interpret/invert.js b/Sprint-2/interpret/invert.js index bb353fb1f..c3addc6d2 100644 --- a/Sprint-2/interpret/invert.js +++ b/Sprint-2/interpret/invert.js @@ -17,13 +17,14 @@ function invert(obj) { } // a) What is the current return value when invert is called with { a : 1 } +//When invert({ a: 1 }) is called, the loop assigns the value 1 to a property literally named key. Therefore, the function returns { 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 key value pairs in separate arrays. It's needed in this program so we can access the keys and values separately. // d) Explain why the current return value is different from the target output - +// It uses dot notation (invertedObj.key), which creates a property called key. It doesn't use the original value as the new property name, so the keys and values aren't swapped correctly. // e) Fix the implementation of invert (and write tests to prove it's fixed!) From 13a4171e0315645029ce2daa798331d6723f9371 Mon Sep 17 00:00:00 2001 From: marscancode Date: Mon, 16 Nov 2026 10:06:13 +0000 Subject: [PATCH 30/32] implemented invert to swap object keys and values --- Sprint-2/interpret/invert.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Sprint-2/interpret/invert.js b/Sprint-2/interpret/invert.js index c3addc6d2..107797a77 100644 --- a/Sprint-2/interpret/invert.js +++ b/Sprint-2/interpret/invert.js @@ -10,12 +10,14 @@ 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 } //When invert({ a: 1 }) is called, the loop assigns the value 1 to a property literally named key. Therefore, the function returns { key: 1 }. From f1cbb6240fa59b48c2220f934fd074d61aa824cc Mon Sep 17 00:00:00 2001 From: marscancode Date: Mon, 16 Nov 2026 10:14:20 +0000 Subject: [PATCH 31/32] Tests to check invert handles multiple key-value pairs --- Sprint-2/interpret/invert.test.js | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 Sprint-2/interpret/invert.test.js diff --git a/Sprint-2/interpret/invert.test.js b/Sprint-2/interpret/invert.test.js new file mode 100644 index 000000000..50b2664b9 --- /dev/null +++ b/Sprint-2/interpret/invert.test.js @@ -0,0 +1,10 @@ +const invert = require("./invert.js"); + +test("given a key-value pair, should return it inverted", () => { + expect(invert({ a: 1 })).toEqual({ 1: "a" }); +}); + +test("given multiple key-value pairs, should return them inverted", () => { + expect(invert({ a: 1, b: 2 })).toEqual({ 1: "a", 2: "b" }); +}); + From ca51a9f4cc94ee3c37b9f8104eda89d01688c36e Mon Sep 17 00:00:00 2001 From: marscancode Date: Mon, 16 Nov 2026 10:16:56 +0000 Subject: [PATCH 32/32] test to check if invert handles empty objects --- Sprint-2/interpret/invert.test.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Sprint-2/interpret/invert.test.js b/Sprint-2/interpret/invert.test.js index 50b2664b9..705d51ce7 100644 --- a/Sprint-2/interpret/invert.test.js +++ b/Sprint-2/interpret/invert.test.js @@ -8,3 +8,6 @@ test("given multiple key-value pairs, should return them inverted", () => { expect(invert({ a: 1, b: 2 })).toEqual({ 1: "a", 2: "b" }); }); +test("given an empty object, should return an empty object", () => { + expect(invert({})).toEqual({}); +});