From 745ed7798fc44f0c530fbafc21ea97b506d96bfe Mon Sep 17 00:00:00 2001 From: iswat Date: Mon, 10 Nov 2025 17:42:25 +0000 Subject: [PATCH 01/31] Predict and explain the code output; fix code where needed --- Sprint-2/debug/address.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Sprint-2/debug/address.js b/Sprint-2/debug/address.js index 940a6af83..b036b4802 100644 --- a/Sprint-2/debug/address.js +++ b/Sprint-2/debug/address.js @@ -1,4 +1,5 @@ // Predict and explain first... +// when the console.log runs, it will return `My house number is undefined` because `0` is not a key in the object so its value will be undefined // This code should log out the houseNumber from the address object // but it isn't working... @@ -12,4 +13,4 @@ const address = { postcode: "XYZ 123", }; -console.log(`My house number is ${address[0]}`); +console.log(`My house number is ${address["houseNumber"]}`); From b63e8a86e43f9226760f89a2f5e73f973ae99c0b Mon Sep 17 00:00:00 2001 From: iswat Date: Mon, 10 Nov 2025 20:45:37 +0000 Subject: [PATCH 02/31] Predict and explain the output, and modify the loop from for...in to for...of --- Sprint-2/debug/author.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Sprint-2/debug/author.js b/Sprint-2/debug/author.js index 8c2125977..969c2ace1 100644 --- a/Sprint-2/debug/author.js +++ b/Sprint-2/debug/author.js @@ -1,4 +1,5 @@ // Predict and explain first... +// When the code runs, the console.log will output an error in the terminal because you can not use for...of loop for an object, instead, you use for...in loop // 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 (const value in author) { + console.log(author[value]); } From 554168fc60010886854df0cfc5e3d038d8878af0 Mon Sep 17 00:00:00 2001 From: iswat Date: Tue, 11 Nov 2025 12:09:07 +0000 Subject: [PATCH 03/31] Predict output and fix code to display each ingredient value on a new line --- Sprint-2/debug/recipe.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/Sprint-2/debug/recipe.js b/Sprint-2/debug/recipe.js index 6cbdd22cd..e800da778 100644 --- a/Sprint-2/debug/recipe.js +++ b/Sprint-2/debug/recipe.js @@ -1,5 +1,5 @@ // Predict and explain first... - +// When the code runs, `${recipe}` part of the console.log expression will output the string `[object Object]` // 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? @@ -11,5 +11,8 @@ const recipe = { }; console.log(`${recipe.title} serves ${recipe.serves} - ingredients: -${recipe}`); + ingredients:`); + +for (const ingredient of recipe.ingredients) { + console.log(ingredient); +} From cf6dae41f05ea883a9a1b57ad12aee5e1711fc29 Mon Sep 17 00:00:00 2001 From: iswat Date: Wed, 12 Nov 2025 14:09:50 +0000 Subject: [PATCH 04/31] Add 'Answer:' clarification comments for code behavior --- Sprint-2/debug/address.js | 2 +- Sprint-2/debug/author.js | 3 ++- Sprint-2/debug/recipe.js | 3 ++- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/Sprint-2/debug/address.js b/Sprint-2/debug/address.js index b036b4802..9c79c04d9 100644 --- a/Sprint-2/debug/address.js +++ b/Sprint-2/debug/address.js @@ -1,5 +1,5 @@ // Predict and explain first... -// when the console.log runs, it will return `My house number is undefined` because `0` is not a key in the object so its value will be undefined +// Answer: when the console.log runs, it will return `My house number is undefined` because `0` is not a key in the object so its value will be undefined // This code should log out the houseNumber from the address object // but it isn't working... diff --git a/Sprint-2/debug/author.js b/Sprint-2/debug/author.js index 969c2ace1..f46621c84 100644 --- a/Sprint-2/debug/author.js +++ b/Sprint-2/debug/author.js @@ -1,5 +1,6 @@ // Predict and explain first... -// When the code runs, the console.log will output an error in the terminal because you can not use for...of loop for an object, instead, you use for...in loop +// Answer: When the code runs, the console.log will output an error in the terminal because you can not use for...of loop for an object, +// instead, you use for...in loop // 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 diff --git a/Sprint-2/debug/recipe.js b/Sprint-2/debug/recipe.js index e800da778..3ea4fad16 100644 --- a/Sprint-2/debug/recipe.js +++ b/Sprint-2/debug/recipe.js @@ -1,5 +1,6 @@ // Predict and explain first... -// When the code runs, `${recipe}` part of the console.log expression will output the string `[object Object]` +// Answer: When the code runs, `${recipe}` part of the console.log expression will output the string `[object Object]` + // 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? From 0601e1a14bde664e926bceceaa47d7f2a2d8b941 Mon Sep 17 00:00:00 2001 From: iswat Date: Thu, 13 Nov 2025 00:29:05 +0000 Subject: [PATCH 05/31] Add test cases for 'contains' function covering valid, empty, and invalid inputs --- Sprint-2/implement/contains.test.js | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/Sprint-2/implement/contains.test.js b/Sprint-2/implement/contains.test.js index 326bdb1f2..08f282ec0 100644 --- a/Sprint-2/implement/contains.test.js +++ b/Sprint-2/implement/contains.test.js @@ -16,20 +16,40 @@ 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("Given an input of an object with a property name, returns true if the object contains the property", () => { + const objInput = {a: 1}; + expect(contains(objInput, "a")).toBe(true); + expect(contains(objInput, "t")).toBe(false); +}) // Given an empty object // When passed to contains // Then it should return false -test.todo("contains on empty object returns false"); +test("Given an empty object, when passed to contains, returns false", () => { + const objInput = {}; + expect(contains(objInput, "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, when passed to contains with an existing property name, returns true", () => { + const objInput = { name: "Alice", age: 25}; + expect(contains(objInput, "age")).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, when passed to contains with a non-existent property name, returns false", () => { + const objInput = { name: "Peter", age: 48}; + expect(contains(objInput, "gender")).toBe(false); +}) // Given invalid parameters like an array // When passed to contains // Then it should return false or throw an error +test("Given invalid parameters, when passed to contains, throws an error", () => { + const objInput = "hello"; + expect(() => (contains(objInput, "location"))).toThrow(new TypeError("Invalid input: obj must be a plain object")); +}) \ No newline at end of file From d5f1bae2311df0e4c7d6d035b072cc9cb62acb42 Mon Sep 17 00:00:00 2001 From: iswat Date: Thu, 13 Nov 2025 00:43:58 +0000 Subject: [PATCH 06/31] Implement 'contains' function: - returns true if object has property - returns false if property missing or object empty - throws TypeError for invalid inputs --- Sprint-2/implement/contains.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Sprint-2/implement/contains.js b/Sprint-2/implement/contains.js index cd779308a..ba646f695 100644 --- a/Sprint-2/implement/contains.js +++ b/Sprint-2/implement/contains.js @@ -1,3 +1,9 @@ -function contains() {} +function contains(obj, propertyName) { + if (typeof(obj) !== "object" || obj === null || Array.isArray(obj)) { + throw new TypeError("Invalid input: obj must be a plain object"); + } + + return obj.hasOwnProperty(propertyName); +} module.exports = contains; From fbc10ecb08efb29ef63b25058d8da84c5d59539f Mon Sep 17 00:00:00 2001 From: iswat Date: Fri, 14 Nov 2025 11:32:26 +0000 Subject: [PATCH 07/31] Add additional tests to each scenario to verify 'contains' function behaves correctly --- Sprint-2/implement/contains.test.js | 57 +++++++++++++++++++++-------- 1 file changed, 41 insertions(+), 16 deletions(-) diff --git a/Sprint-2/implement/contains.test.js b/Sprint-2/implement/contains.test.js index 08f282ec0..ecc77b865 100644 --- a/Sprint-2/implement/contains.test.js +++ b/Sprint-2/implement/contains.test.js @@ -17,39 +17,64 @@ as the object doesn't contains a key of 'c' // When passed an object and a property name // Then it should return true if the object contains the property, false otherwise test("Given an input of an object with a property name, returns true if the object contains the property", () => { - const objInput = {a: 1}; - expect(contains(objInput, "a")).toBe(true); - expect(contains(objInput, "t")).toBe(false); -}) + const objInput = { a: 1 }; + expect(contains(objInput, "a")).toBe(true); + expect(contains(objInput, "t")).toBe(false); +}); // Given an empty object // When passed to contains // Then it should return false test("Given an empty object, when passed to contains, returns false", () => { - const objInput = {}; - expect(contains(objInput, "a")).toBe(false); -}) + const objInput = {}; + expect(contains(objInput, "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, when passed to contains with an existing property name, returns true", () => { - const objInput = { name: "Alice", age: 25}; - expect(contains(objInput, "age")).toBe(true); -}) + const objInput = { name: "Alice", age: 25 }; + expect(contains(objInput, "age")).toBe(true); + + const edgeCaseInput = { 1: "one", "": "empty" }; + expect(contains(edgeCaseInput, "")).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, when passed to contains with a non-existent property name, returns false", () => { - const objInput = { name: "Peter", age: 48}; - expect(contains(objInput, "gender")).toBe(false); -}) + const objInput = { name: "Peter", age: 48 }; + expect(contains(objInput, "gender")).toBe(false); + + const objInput1= { name: "Tayo", age: 10 }; + expect(contains(objInput1, "location")).toBe(false); +}); // Given invalid parameters like an array // When passed to contains // Then it should return false or throw an error test("Given invalid parameters, when passed to contains, throws an error", () => { - const objInput = "hello"; - expect(() => (contains(objInput, "location"))).toThrow(new TypeError("Invalid input: obj must be a plain object")); -}) \ No newline at end of file + expect(() => contains("hello", "location")).toThrow( + new TypeError("Invalid input: obj must be a plain object") + ); + expect(() => contains(null, "a")).toThrow( + new TypeError("Invalid input: obj must be a plain object") + ); + expect(() => contains(undefined, "a")).toThrow( + new TypeError("Invalid input: obj must be a plain object") + ); + expect(() => contains(123, "a")).toThrow( + new TypeError("Invalid input: obj must be a plain object") + ); + expect(() => contains(true, "a")).toThrow( + new TypeError("Invalid input: obj must be a plain object") + ); + expect(() => contains([], "a")).toThrow( + new TypeError("Invalid input: obj must be a plain object") + ); + expect(() => contains(() => {}, "a")).toThrow( + new TypeError("Invalid input: obj must be a plain object") + ); +}); From 70dc26cdeae25d02cb38419152c8f766750e1f0d Mon Sep 17 00:00:00 2001 From: iswat Date: Fri, 14 Nov 2025 11:34:20 +0000 Subject: [PATCH 08/31] Fix indentation inconsistencies in 'contains' function --- Sprint-2/implement/contains.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Sprint-2/implement/contains.js b/Sprint-2/implement/contains.js index ba646f695..6431d8884 100644 --- a/Sprint-2/implement/contains.js +++ b/Sprint-2/implement/contains.js @@ -1,9 +1,9 @@ function contains(obj, propertyName) { - if (typeof(obj) !== "object" || obj === null || Array.isArray(obj)) { - throw new TypeError("Invalid input: obj must be a plain object"); - } + if (typeof obj !== "object" || obj === null || Array.isArray(obj)) { + throw new TypeError("Invalid input: obj must be a plain object"); + } - return obj.hasOwnProperty(propertyName); + return obj.hasOwnProperty(propertyName); } module.exports = contains; From 829f63d0669578b6a82d4fcf447a7e0947f455df Mon Sep 17 00:00:00 2001 From: iswat Date: Fri, 14 Nov 2025 11:51:19 +0000 Subject: [PATCH 09/31] Remove .todo and add unit test for createLookup function --- Sprint-2/implement/lookup.test.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/Sprint-2/implement/lookup.test.js b/Sprint-2/implement/lookup.test.js index 547e06c5a..a536af982 100644 --- a/Sprint-2/implement/lookup.test.js +++ b/Sprint-2/implement/lookup.test.js @@ -1,6 +1,16 @@ const createLookup = require("./lookup.js"); -test.todo("creates a country currency code lookup for multiple codes"); +test("creates a country currency code lookup for multiple codes", () => { + const currencyArray = [ + ["US", "USD"], + ["CA", "CAD"], + ]; + const expectedResult = { + US: "USD", + CA: "CAD", + }; + expect(createLookup(currencyArray)).toEqual(expectedResult); +}); /* From 0ffc147c262819563cd1d0356efe1d9d2485872e Mon Sep 17 00:00:00 2001 From: iswat Date: Fri, 14 Nov 2025 12:17:19 +0000 Subject: [PATCH 10/31] Implement createLookup function to convert array of pairs into lookup object --- Sprint-2/implement/lookup.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Sprint-2/implement/lookup.js b/Sprint-2/implement/lookup.js index a6746e07f..a8131fd98 100644 --- a/Sprint-2/implement/lookup.js +++ b/Sprint-2/implement/lookup.js @@ -1,4 +1,10 @@ -function createLookup() { +function createLookup(arrayLookUp) { + const objLookUp = {}; + + for (const [country, currency] of arrayLookUp) { + objLookUp[country] = currency; + } + return objLookUp; // implementation here } From 99df5b64585b6731e06fd880161a99a947f343bf Mon Sep 17 00:00:00 2001 From: iswat Date: Fri, 14 Nov 2025 13:05:56 +0000 Subject: [PATCH 11/31] Fix parseQueryString so key-value pairs with '=' in values are parsed correctly --- Sprint-2/implement/querystring.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/Sprint-2/implement/querystring.js b/Sprint-2/implement/querystring.js index 45ec4e5f3..0847324ce 100644 --- a/Sprint-2/implement/querystring.js +++ b/Sprint-2/implement/querystring.js @@ -6,11 +6,21 @@ function parseQueryString(queryString) { const keyValuePairs = queryString.split("&"); for (const pair of keyValuePairs) { - const [key, value] = pair.split("="); + const index = pair.indexOf("="); + let key, value; + + if (index === -1) { + key = pair; + value = ""; + } else { + key = pair.slice(0, index); + value = pair.slice(index + 1); + } queryParams[key] = value; } return queryParams; } + module.exports = parseQueryString; From 60f6d2105ff9625e5ffb30f2cf09047e024168f1 Mon Sep 17 00:00:00 2001 From: iswat Date: Fri, 14 Nov 2025 13:22:54 +0000 Subject: [PATCH 12/31] Fix parseQueryString to throw TypeError for non-string input --- Sprint-2/implement/querystring.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Sprint-2/implement/querystring.js b/Sprint-2/implement/querystring.js index 0847324ce..c4f285dbf 100644 --- a/Sprint-2/implement/querystring.js +++ b/Sprint-2/implement/querystring.js @@ -1,4 +1,7 @@ function parseQueryString(queryString) { + if (typeof queryString !== "string") { + throw new TypeError("Invalid input: Input must be a string"); + } const queryParams = {}; if (queryString.length === 0) { return queryParams; From 7bb207451b7e0188a07bd29bbc873dc1a45cc396 Mon Sep 17 00:00:00 2001 From: iswat Date: Fri, 14 Nov 2025 13:24:29 +0000 Subject: [PATCH 13/31] Add test cases covering edge cases for parseQueryString --- Sprint-2/implement/querystring.test.js | 73 +++++++++++++++++++++++++- 1 file changed, 71 insertions(+), 2 deletions(-) diff --git a/Sprint-2/implement/querystring.test.js b/Sprint-2/implement/querystring.test.js index 3e218b789..6fd81dc19 100644 --- a/Sprint-2/implement/querystring.test.js +++ b/Sprint-2/implement/querystring.test.js @@ -3,10 +3,79 @@ // 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 =", () => { expect(parseQueryString("equation=x=y+1")).toEqual({ - "equation": "x=y+1", + equation: "x=y+1", + }); +}); + +test("handles empty query string", () => { + expect(parseQueryString("")).toEqual({}); +}); + +test("handles empty key", () => { + expect(parseQueryString("=value")).toEqual({ + "": "value", + }); +}); + +test("parses multiple key-value pairs", () => { + expect(parseQueryString("a=1&b=2&c=3")).toEqual({ + a: "1", + b: "2", + c: "3", + }); +}); + +test("parses values with spaces or special characters", () => { + expect(parseQueryString("name=John%20Doe&city=New+York")).toEqual({ + name: "John%20Doe", + city: "New+York", + }); +}); + +test("throws TypeError for non-string input", () => { + expect(() => parseQueryString(null)).toThrow(TypeError); + expect(() => parseQueryString(null)).toThrow( + "Invalid input: Input must be a string" + ); + + expect(() => parseQueryString(undefined)).toThrow(TypeError); + expect(() => parseQueryString(undefined)).toThrow( + "Invalid input: Input must be a string" + ); + + expect(() => parseQueryString(123)).toThrow(TypeError); + expect(() => parseQueryString(123)).toThrow( + "Invalid input: Input must be a string" + ); + + expect(() => parseQueryString({})).toThrow(TypeError); + expect(() => parseQueryString({})).toThrow( + "Invalid input: Input must be a string" + ); +}); + +test("handles repeated keys, last value wins", () => { + expect(parseQueryString("a=1&a=2")).toEqual({ + a: "2", + }); +}); + +test("handles trailing '&'", () => { + expect(parseQueryString("a=1&b=2&")).toEqual({ + a: "1", + b: "2", + "": "", // empty pair after trailing & + }); +}); + +test("handles leading '&'", () => { + expect(parseQueryString("&a=1&b=2")).toEqual({ + "": "", // empty pair before leading & + a: "1", + b: "2", }); }); From a2efcdfb49431dadeacab0794bf7c16defb79bab Mon Sep 17 00:00:00 2001 From: iswat Date: Fri, 14 Nov 2025 18:48:47 +0000 Subject: [PATCH 14/31] Add test cases for tally function to cover empty input, duplicates, and invalid input --- Sprint-2/implement/tally.test.js | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/Sprint-2/implement/tally.test.js b/Sprint-2/implement/tally.test.js index 2ceffa8dd..d5a7f02e4 100644 --- a/Sprint-2/implement/tally.test.js +++ b/Sprint-2/implement/tally.test.js @@ -19,16 +19,30 @@ 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 when passed an array of items, returns an object containing counts for each unique item", () => { + expect(tally(["a"])).toEqual({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", () => { + expect(tally([])).toEqual({}); +}); + // Given an array with duplicate items // When passed to tally // Then it should return counts for each unique item +test("when an array with duplicate items is passed to tally", () => { + expect(tally(["a", "a", "b", "c"])).toEqual({ a: 2, b: 1, c: 1 }); +}) // Given an invalid input like a string // When passed to tally // Then it should throw an error +test("when an invalid input is passed to tally, throws an error", () => { + expect(() => tally("house")).toThrow( + new TypeError("Invalid input: Input must be an array") + ); +}) From 156dfd953f7b9eba73981f728ab76db768b18083 Mon Sep 17 00:00:00 2001 From: iswat Date: Fri, 14 Nov 2025 19:51:32 +0000 Subject: [PATCH 15/31] Add function implementation to count array items and handle invalid input --- Sprint-2/implement/tally.js | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/Sprint-2/implement/tally.js b/Sprint-2/implement/tally.js index f47321812..d2bb65861 100644 --- a/Sprint-2/implement/tally.js +++ b/Sprint-2/implement/tally.js @@ -1,3 +1,19 @@ -function tally() {} +function tally(arr) { + if (!Array.isArray(arr)) { + throw new TypeError("Invalid input: Input must be an array"); + } + + const result = {}; + + for (const item of arr) { + if (result[item]) { + result[item] += 1; + } else { + result[item] = 1; + } + } + + return result; +} module.exports = tally; From 1a47dc3a07845b9f5279b9dad5fa23891dabf75e Mon Sep 17 00:00:00 2001 From: iswat Date: Sat, 15 Nov 2025 12:09:30 +0000 Subject: [PATCH 16/31] Add answers to questions and fix invert() implementation to produce correct output --- Sprint-2/interpret/invert.js | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/Sprint-2/interpret/invert.js b/Sprint-2/interpret/invert.js index bb353fb1f..ac505d3f5 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 } - +// Answer: { key: 1} // b) What is the current return value when invert is called with { a: 1, b: 2 } - +// Answer: { key:2} // c) What is the target return value when invert is called with {a : 1, b: 2} - -// c) What does Object.entries return? Why is it needed in this program? - -// d) Explain why the current return value is different from the target output - -// e) Fix the implementation of invert (and write tests to prove it's fixed!) +// Answer: { '1': 'a', '2': 'b' } +// d) What does Object.entries return? Why is it needed in this program? +// Object.entries(obj) returns an array of [key, value] pairs, like: [ [ 'a', 1 ], [ 'b', 2 ] ] +// e) Explain why the current return value is different from the target output +// Answer: The current return value is different from the target output because `invertedObj.key` +// uses the literal string "key" instead of the variable key. To use the variable's value as the property name, +// we must use bracket notation: invertedObj[value] = key. +// f) Fix the implementation of invert (and write tests to prove it's fixed!) From 9f60b8bfc420652712b4b4fb36954f0bc4efd531 Mon Sep 17 00:00:00 2001 From: iswat Date: Sat, 15 Nov 2025 12:32:57 +0000 Subject: [PATCH 17/31] Add test file for invert and test that empty object returns empty object; import invert with require --- Sprint-2/interpret/invert.test.js | 6 ++++++ 1 file changed, 6 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..bf9b47522 --- /dev/null +++ b/Sprint-2/interpret/invert.test.js @@ -0,0 +1,6 @@ +const invert = require("./invert.js"); + +test("Given an empty object, return an empty object", () => { + expect(invert({})).toEqual({}); +}) + From acf14dd51ab2285c696feb728756906cb47ab4ee Mon Sep 17 00:00:00 2001 From: iswat Date: Sat, 15 Nov 2025 12:35:04 +0000 Subject: [PATCH 18/31] Add the module.exports statement so the invert function can be imported by another file --- Sprint-2/interpret/invert.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Sprint-2/interpret/invert.js b/Sprint-2/interpret/invert.js index ac505d3f5..f979291bf 100644 --- a/Sprint-2/interpret/invert.js +++ b/Sprint-2/interpret/invert.js @@ -16,6 +16,8 @@ function invert(obj) { return invertedObj; } +module.exports = invert; + // a) What is the current return value when invert is called with { a : 1 } // Answer: { key: 1} // b) What is the current return value when invert is called with { a: 1, b: 2 } From 92e74343c6bf976bf5f80fb6b52d2436ab8d47be Mon Sep 17 00:00:00 2001 From: iswat Date: Sun, 16 Nov 2025 18:22:03 +0000 Subject: [PATCH 19/31] Add test to verify invert returns an empty object for empty input --- Sprint-2/interpret/invert.test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Sprint-2/interpret/invert.test.js b/Sprint-2/interpret/invert.test.js index bf9b47522..2da01cd20 100644 --- a/Sprint-2/interpret/invert.test.js +++ b/Sprint-2/interpret/invert.test.js @@ -1,6 +1,6 @@ const invert = require("./invert.js"); test("Given an empty object, return an empty object", () => { - expect(invert({})).toEqual({}); -}) + expect(invert({})).toEqual({}); +}); From 7ab4f2da44442cf69b79b717ca8e519acb537f26 Mon Sep 17 00:00:00 2001 From: iswat Date: Sun, 16 Nov 2025 18:22:34 +0000 Subject: [PATCH 20/31] Add test to verify invert correctly swaps a single key-value pair --- 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 2da01cd20..526a7144a 100644 --- a/Sprint-2/interpret/invert.test.js +++ b/Sprint-2/interpret/invert.test.js @@ -4,3 +4,6 @@ test("Given an empty object, return an empty object", () => { expect(invert({})).toEqual({}); }); +test("Given a single key-value pair object, returns a swap value", () => { + expect(invert({ a: 1 })).toEqual({ 1: "a" }); +}); From c2f3fd2b0fe4c38b18314670bcc0b9a75d4cc86a Mon Sep 17 00:00:00 2001 From: iswat Date: Sun, 16 Nov 2025 18:23:11 +0000 Subject: [PATCH 21/31] Add test to verify invert swaps multiple key-value pairs --- Sprint-2/interpret/invert.test.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Sprint-2/interpret/invert.test.js b/Sprint-2/interpret/invert.test.js index 526a7144a..6b93f771a 100644 --- a/Sprint-2/interpret/invert.test.js +++ b/Sprint-2/interpret/invert.test.js @@ -7,3 +7,7 @@ test("Given an empty object, return an empty object", () => { test("Given a single key-value pair object, returns a swap value", () => { expect(invert({ a: 1 })).toEqual({ 1: "a" }); }); + +test("Given multiple key-value pairs object, returns a swap value", () => { + expect(invert({ a: 1, b: 2 })).toEqual({ 1: "a", 2: "b" }); +}); From a6a37dcf5d75dbc4ba5ca16d0bff83b225d3d8ae Mon Sep 17 00:00:00 2001 From: iswat Date: Sun, 16 Nov 2025 18:46:31 +0000 Subject: [PATCH 22/31] Add test to verify invert throws a TypeError when given invalid input --- Sprint-2/interpret/invert.test.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Sprint-2/interpret/invert.test.js b/Sprint-2/interpret/invert.test.js index 6b93f771a..8435e325a 100644 --- a/Sprint-2/interpret/invert.test.js +++ b/Sprint-2/interpret/invert.test.js @@ -11,3 +11,9 @@ test("Given a single key-value pair object, returns a swap value", () => { test("Given multiple key-value pairs object, returns a swap value", () => { expect(invert({ a: 1, b: 2 })).toEqual({ 1: "a", 2: "b" }); }); + +test("Given an input that is not a plain object, throws an error", () => { + expect(() => invert(null)).toThrowError( + new TypeError("Invalid input: Input must be a plain object") + ); +}); From abaa0f855223b304ba553fb310dbed7b6f527d78 Mon Sep 17 00:00:00 2001 From: iswat Date: Sun, 16 Nov 2025 18:49:36 +0000 Subject: [PATCH 23/31] Fix invert to throw an error for non-plain object input --- 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 f979291bf..da315753a 100644 --- a/Sprint-2/interpret/invert.js +++ b/Sprint-2/interpret/invert.js @@ -8,7 +8,9 @@ function invert(obj) { const invertedObj = {}; - + if (Object.prototype.toString.call(obj) !== "[object Object]") { + throw new TypeError("Invalid input: Input must be a plain object"); + } for (const [key, value] of Object.entries(obj)) { invertedObj[value] = key; } From bcdb27009687cb35c9f8a8facc3398866fb9da45 Mon Sep 17 00:00:00 2001 From: iswat Date: Sun, 16 Nov 2025 19:32:14 +0000 Subject: [PATCH 24/31] Add additional tests for empty, single, and multiple key-value pairs, and nested object input --- Sprint-2/interpret/invert.test.js | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/Sprint-2/interpret/invert.test.js b/Sprint-2/interpret/invert.test.js index 8435e325a..9ce03a6cd 100644 --- a/Sprint-2/interpret/invert.test.js +++ b/Sprint-2/interpret/invert.test.js @@ -6,14 +6,42 @@ test("Given an empty object, return an empty object", () => { test("Given a single key-value pair object, returns a swap value", () => { expect(invert({ a: 1 })).toEqual({ 1: "a" }); + expect(invert({ 1: 1 })).toEqual({ 1: "1" }); + expect(invert({ key: true })).toEqual({ true: "key" }); }); test("Given multiple key-value pairs object, returns a swap value", () => { expect(invert({ a: 1, b: 2 })).toEqual({ 1: "a", 2: "b" }); + expect(invert({ 1: "a", 2: "b" })).toEqual({ a: "1", b: "2" }); + expect(invert({ a: 1, b: "x", c: true })).toEqual({ + 1: "a", + x: "b", + true: "c", + }); + expect(invert({ a: 1, b: 1 })).toEqual({ 1: "b" }); + expect(invert({ a: 1, b: 1, c: 1 })).toEqual({ 1: "c" }); }); test("Given an input that is not a plain object, throws an error", () => { expect(() => invert(null)).toThrowError( new TypeError("Invalid input: Input must be a plain object") ); + expect(() => invert(undefined)).toThrowError( + new TypeError("Invalid input: Input must be a plain object") + ); + expect(() => invert(true)).toThrowError( + new TypeError("Invalid input: Input must be a plain object") + ); + expect(() => invert(null)).toThrowError( + new TypeError("Invalid input: Input must be a plain object") + ); }); + +test("handles nested objects and arrays as values", () => { + const nestedObj = { a: { x: 1 } }; + const nestedArray = { b: [1, 2, 3] }; + // Nested object key becomes "[object Object]" + expect(invert(nestedObj)).toEqual({ "[object Object]": "a" }); + // Array key becomes "1,2,3" + expect(invert(nestedArray)).toEqual({ "1,2,3": "b" }); +}); \ No newline at end of file From 399fbc8ebf0d4d8a41927b12da45018ebe11f19d Mon Sep 17 00:00:00 2001 From: iswat Date: Fri, 21 Nov 2025 18:12:20 +0000 Subject: [PATCH 25/31] Use Object.create(null) in tally to avoid inherited properties like toString affecting expected results --- Sprint-2/implement/tally.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sprint-2/implement/tally.js b/Sprint-2/implement/tally.js index d2bb65861..65263d045 100644 --- a/Sprint-2/implement/tally.js +++ b/Sprint-2/implement/tally.js @@ -3,7 +3,7 @@ function tally(arr) { throw new TypeError("Invalid input: Input must be an array"); } - const result = {}; + const result = Object.create(null); for (const item of arr) { if (result[item]) { From 368565226d00a199b755d01f79b4e2cbd3cb4379 Mon Sep 17 00:00:00 2001 From: iswat Date: Fri, 21 Nov 2025 18:17:24 +0000 Subject: [PATCH 26/31] Add test case demonstrating tally must handle items like "toString" without prototype conflicts --- Sprint-2/implement/tally.test.js | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/Sprint-2/implement/tally.test.js b/Sprint-2/implement/tally.test.js index d5a7f02e4..ca94edf40 100644 --- a/Sprint-2/implement/tally.test.js +++ b/Sprint-2/implement/tally.test.js @@ -20,29 +20,32 @@ 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 when passed an array of items, returns an object containing counts for each unique item", () => { - expect(tally(["a"])).toEqual({a:1}); -}) + expect(tally(["a"])).toEqual({ a: 1 }); +}); // Given an empty array // When passed to tally // Then it should return an empty object test("tally on an empty array returns an empty object", () => { - expect(tally([])).toEqual({}); + expect(tally([])).toEqual({}); }); - // Given an array with duplicate items // When passed to tally // Then it should return counts for each unique item test("when an array with duplicate items is passed to tally", () => { - expect(tally(["a", "a", "b", "c"])).toEqual({ a: 2, b: 1, c: 1 }); -}) + expect(tally(["a", "a", "b", "c"])).toEqual({ a: 2, b: 1, c: 1 }); +}); // Given an invalid input like a string // When passed to tally // Then it should throw an error test("when an invalid input is passed to tally, throws an error", () => { - expect(() => tally("house")).toThrow( - new TypeError("Invalid input: Input must be an array") - ); -}) + expect(() => tally("house")).toThrow( + new TypeError("Invalid input: Input must be an array") + ); +}); + +test("tally handles items that match inherited object properties", () => { + expect(tally(["toString", "toString"])).toEqual({ toString: 2 }); +}); From 5fcad91ddcc3fd952911a6aa4d345e6899f649ee Mon Sep 17 00:00:00 2001 From: iswat Date: Fri, 21 Nov 2025 18:42:34 +0000 Subject: [PATCH 27/31] Add conditional decode step for safely handling encoded query strings --- Sprint-2/implement/querystring.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Sprint-2/implement/querystring.js b/Sprint-2/implement/querystring.js index c4f285dbf..276093070 100644 --- a/Sprint-2/implement/querystring.js +++ b/Sprint-2/implement/querystring.js @@ -6,6 +6,11 @@ function parseQueryString(queryString) { if (queryString.length === 0) { return queryParams; } + + if (decodeURI(queryString) === decodeURIComponent(queryString)) { + queryString = decodeURIComponent(queryString); + } + const keyValuePairs = queryString.split("&"); for (const pair of keyValuePairs) { @@ -25,5 +30,4 @@ function parseQueryString(queryString) { return queryParams; } - module.exports = parseQueryString; From 224597c46fb3ee5bea96df22c97d015cb55082a6 Mon Sep 17 00:00:00 2001 From: iswat Date: Fri, 21 Nov 2025 18:56:32 +0000 Subject: [PATCH 28/31] Add tests to ensure parseQueryString handles encoded spaces and special characters --- Sprint-2/implement/querystring.test.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Sprint-2/implement/querystring.test.js b/Sprint-2/implement/querystring.test.js index 6fd81dc19..5086ac04b 100644 --- a/Sprint-2/implement/querystring.test.js +++ b/Sprint-2/implement/querystring.test.js @@ -31,9 +31,12 @@ test("parses multiple key-value pairs", () => { test("parses values with spaces or special characters", () => { expect(parseQueryString("name=John%20Doe&city=New+York")).toEqual({ - name: "John%20Doe", + name: "John Doe", city: "New+York", }); + expect(parseQueryString("tags%5B%5D=hello%20world")).toEqual({ + "tags[]": "hello world", + }); }); test("throws TypeError for non-string input", () => { From 3578991880f7e37a6201ba1971b3aeee2ff7fe30 Mon Sep 17 00:00:00 2001 From: iswat Date: Fri, 21 Nov 2025 19:00:49 +0000 Subject: [PATCH 29/31] Refactor contains test to remove repetition using a loop over invalid inputs --- Sprint-2/implement/contains.test.js | 32 +++++++++-------------------- 1 file changed, 10 insertions(+), 22 deletions(-) diff --git a/Sprint-2/implement/contains.test.js b/Sprint-2/implement/contains.test.js index ecc77b865..08086d2fa 100644 --- a/Sprint-2/implement/contains.test.js +++ b/Sprint-2/implement/contains.test.js @@ -55,26 +55,14 @@ test("Given an object with properties, when passed to contains with a non-existe // Given invalid parameters like an array // When passed to contains // Then it should return false or throw an error -test("Given invalid parameters, when passed to contains, throws an error", () => { - expect(() => contains("hello", "location")).toThrow( - new TypeError("Invalid input: obj must be a plain object") - ); - expect(() => contains(null, "a")).toThrow( - new TypeError("Invalid input: obj must be a plain object") - ); - expect(() => contains(undefined, "a")).toThrow( - new TypeError("Invalid input: obj must be a plain object") - ); - expect(() => contains(123, "a")).toThrow( - new TypeError("Invalid input: obj must be a plain object") - ); - expect(() => contains(true, "a")).toThrow( - new TypeError("Invalid input: obj must be a plain object") - ); - expect(() => contains([], "a")).toThrow( - new TypeError("Invalid input: obj must be a plain object") - ); - expect(() => contains(() => {}, "a")).toThrow( - new TypeError("Invalid input: obj must be a plain object") - ); +test("Given invalid parameters, contains throws a TypeError", () => { + const invalidInputs = ["hello", null, undefined, 123, true, [], () => {}]; + + invalidInputs.forEach(input => { + expect(() => contains(input, "a")).toThrow( + new TypeError("Invalid input: obj must be a plain object") + ); + }); }); + + From 0f518e295bac81c64defe59020f858bb2f9f6cd4 Mon Sep 17 00:00:00 2001 From: iswat Date: Fri, 21 Nov 2025 19:08:48 +0000 Subject: [PATCH 30/31] Replace loop with Array.prototype.join() to print ingredients in one console.log --- Sprint-2/debug/recipe.js | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/Sprint-2/debug/recipe.js b/Sprint-2/debug/recipe.js index 3ea4fad16..5020d1602 100644 --- a/Sprint-2/debug/recipe.js +++ b/Sprint-2/debug/recipe.js @@ -12,8 +12,5 @@ const recipe = { }; console.log(`${recipe.title} serves ${recipe.serves} - ingredients:`); - -for (const ingredient of recipe.ingredients) { - console.log(ingredient); -} + ingredients: +${recipe.ingredients.join("\n")}`); From 3f29b47ede95b6cb1835fa082ed24540617fd96b Mon Sep 17 00:00:00 2001 From: iswat Date: Fri, 21 Nov 2025 19:53:31 +0000 Subject: [PATCH 31/31] Fix parseQueryString to decode keys and values safely after splitting and throw descriptive error for invalid URL encoding --- Sprint-2/implement/querystring.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/Sprint-2/implement/querystring.js b/Sprint-2/implement/querystring.js index 276093070..b496db76c 100644 --- a/Sprint-2/implement/querystring.js +++ b/Sprint-2/implement/querystring.js @@ -7,10 +7,6 @@ function parseQueryString(queryString) { return queryParams; } - if (decodeURI(queryString) === decodeURIComponent(queryString)) { - queryString = decodeURIComponent(queryString); - } - const keyValuePairs = queryString.split("&"); for (const pair of keyValuePairs) { @@ -24,6 +20,14 @@ function parseQueryString(queryString) { key = pair.slice(0, index); value = pair.slice(index + 1); } + + try { + key = decodeURIComponent(key); + value = decodeURIComponent(value); + } catch (err) { + throw new Error("Invalid URL-encoded string"); + } + queryParams[key] = value; }