From 86f5c2a4c75126ecd5e9e0b2a73a096ffda90f8c Mon Sep 17 00:00:00 2001 From: Elisabeth-Matulian Date: Fri, 6 Mar 2026 06:44:57 +0000 Subject: [PATCH 1/3] bugs fixed for debug directory --- Sprint-2/debug/address.js | 3 ++- Sprint-2/debug/author.js | 6 ++++-- Sprint-2/debug/recipe.js | 3 ++- Sprint-2/package-lock.json | 2 ++ 4 files changed, 10 insertions(+), 4 deletions(-) diff --git a/Sprint-2/debug/address.js b/Sprint-2/debug/address.js index 940a6af83..fd4c7606e 100644 --- a/Sprint-2/debug/address.js +++ b/Sprint-2/debug/address.js @@ -1,4 +1,5 @@ // Predict and explain first... +//The console.log will log out undefined because there is no such a key as 0 in the address object // 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}`); diff --git a/Sprint-2/debug/author.js b/Sprint-2/debug/author.js index 8c2125977..e53d20ae5 100644 --- a/Sprint-2/debug/author.js +++ b/Sprint-2/debug/author.js @@ -1,4 +1,6 @@ // Predict and explain first... +//1st prediction: var value will log out both-key and value and we need value only +//2nd prediction(as the 1st was wrong): MDN says that for...of doen't work with objects but for...in does // 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 +13,6 @@ const author = { alive: true, }; -for (const value of author) { - console.log(value); +for (const value in author) { + console.log(author[value]); } diff --git a/Sprint-2/debug/recipe.js b/Sprint-2/debug/recipe.js index 6cbdd22cd..4df8d5ea1 100644 --- a/Sprint-2/debug/recipe.js +++ b/Sprint-2/debug/recipe.js @@ -1,4 +1,5 @@ // Predict and explain first... +// the issue here is with line 16 ${recipe}. I guess it will log out thw whole content of the recipe object, both key and value. // This program should log out the title, how many it serves and the ingredients. // Each ingredient should be logged on a new line @@ -12,4 +13,4 @@ const recipe = { console.log(`${recipe.title} serves ${recipe.serves} ingredients: -${recipe}`); +${recipe.ingredients.join('\n')}`); diff --git a/Sprint-2/package-lock.json b/Sprint-2/package-lock.json index 9b4c725d6..ceda7296e 100644 --- a/Sprint-2/package-lock.json +++ b/Sprint-2/package-lock.json @@ -56,6 +56,7 @@ "integrity": "sha512-Oixnb+DzmRT30qu9d3tJSQkxuygWm32DFykT4bRoORPa9hZ/L4KhVB/XiRm6KG+roIEM7DBQlmg27kw2HZkdZg==", "dev": true, "license": "MIT", + "peer": true, "dependencies": { "@ampproject/remapping": "^2.2.0", "@babel/code-frame": "^7.25.7", @@ -1368,6 +1369,7 @@ } ], "license": "MIT", + "peer": true, "dependencies": { "caniuse-lite": "^1.0.30001663", "electron-to-chromium": "^1.5.28", From f0bcfcd37966129f16a6ce2832eef81c0ed34b43 Mon Sep 17 00:00:00 2001 From: Elisabeth-Matulian Date: Fri, 6 Mar 2026 17:37:47 +0000 Subject: [PATCH 2/3] complete implement exercises --- Sprint-2/implement/contains.js | 10 +++++++--- Sprint-2/implement/contains.test.js | 15 ++++++++++++++- Sprint-2/implement/lookup.js | 9 +++++++-- Sprint-2/implement/lookup.test.js | 6 ++++-- Sprint-2/implement/querystring.js | 8 ++++++-- Sprint-2/implement/querystring.test.js | 13 +++++++++++++ Sprint-2/implement/tally.js | 13 ++++++++++++- Sprint-2/implement/tally.test.js | 11 ++++++++++- 8 files changed, 73 insertions(+), 12 deletions(-) diff --git a/Sprint-2/implement/contains.js b/Sprint-2/implement/contains.js index cd779308a..b6bd92473 100644 --- a/Sprint-2/implement/contains.js +++ b/Sprint-2/implement/contains.js @@ -1,3 +1,7 @@ -function contains() {} - -module.exports = contains; +function contains(object, property) { + if (typeof object === 'object' && false === Array.isArray(object)) { + return object.hasOwnProperty(property); + } + else throw new Error("Not an object"); +} +module.exports = contains; \ No newline at end of file diff --git a/Sprint-2/implement/contains.test.js b/Sprint-2/implement/contains.test.js index 326bdb1f2..cb4b6e362 100644 --- a/Sprint-2/implement/contains.test.js +++ b/Sprint-2/implement/contains.test.js @@ -16,20 +16,33 @@ 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.todo("contains on empty object returns false"); // Given an empty object // When passed to contains // Then it should return false -test.todo("contains on empty object returns false"); +test("returns false for 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("returns true for object that contains an existing property name", () => { + 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("returns false for object that contains a non-existent property name", () => { + 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("it will throw an error parameters are invalid", () => { + expect(() => contains([])).toThrow(Error); +}); + diff --git a/Sprint-2/implement/lookup.js b/Sprint-2/implement/lookup.js index a6746e07f..614852a33 100644 --- a/Sprint-2/implement/lookup.js +++ b/Sprint-2/implement/lookup.js @@ -1,5 +1,10 @@ -function createLookup() { - // implementation here +function createLookup(array) { + let object = {}; + for (const pair of array) { + object[pair[0]] = pair[1]; + } + return object } module.exports = createLookup; + diff --git a/Sprint-2/implement/lookup.test.js b/Sprint-2/implement/lookup.test.js index 547e06c5a..f441f9ed5 100644 --- a/Sprint-2/implement/lookup.test.js +++ b/Sprint-2/implement/lookup.test.js @@ -1,7 +1,9 @@ const createLookup = require("./lookup.js"); -test.todo("creates a country currency code lookup for multiple codes"); - +//test.todo("creates a country currency code lookup for multiple codes"); +test("creates a country currency code lookup for multiple codes", () => { + expect(createLookup([['US', 'USD'], ['CA', 'CAD']])).toEqual({'US': 'USD', 'CA': 'CAD'}); +}); /* 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..2d985b2eb 100644 --- a/Sprint-2/implement/querystring.js +++ b/Sprint-2/implement/querystring.js @@ -6,8 +6,12 @@ function parseQueryString(queryString) { const keyValuePairs = queryString.split("&"); for (const pair of keyValuePairs) { - const [key, value] = pair.split("="); - queryParams[key] = value; + if (pair.includes('=')){ + const index = pair.indexOf("="); + const key = pair.slice(0, index); + const value = pair.slice(index+1); + queryParams[key] = value; + } } return queryParams; diff --git a/Sprint-2/implement/querystring.test.js b/Sprint-2/implement/querystring.test.js index 3e218b789..276f8f02b 100644 --- a/Sprint-2/implement/querystring.test.js +++ b/Sprint-2/implement/querystring.test.js @@ -10,3 +10,16 @@ test("parses querystring values containing =", () => { "equation": "x=y+1", }); }); + +test("parses querystring values doesn't contain =", () => { + expect(parseQueryString("equationxy1")).toEqual({}); +}); + +test("given a query string with no query parameters, returns an empty object", () => { + expect(parseQueryString("")).toEqual({}); +}); + +test("given a query string with multiple key-value pairs, returns them in object form", () => { + expect(parseQueryString("sort=lowest&colour=yellow")).toEqual({sort: "lowest", colour: "yellow"}); +}); + diff --git a/Sprint-2/implement/tally.js b/Sprint-2/implement/tally.js index f47321812..2364884ac 100644 --- a/Sprint-2/implement/tally.js +++ b/Sprint-2/implement/tally.js @@ -1,3 +1,14 @@ -function tally() {} +function tally(array) { + let object = {}; + if (Array.isArray(array)) { + for (const i of array) { + if (object.hasOwnProperty(i)) { + object[i] = object[i] + 1; + } + else object[i] = 1; + } + return object;} + else throw new Error("Invalid input"); +} module.exports = tally; diff --git a/Sprint-2/implement/tally.test.js b/Sprint-2/implement/tally.test.js index 2ceffa8dd..d3a9560cc 100644 --- a/Sprint-2/implement/tally.test.js +++ b/Sprint-2/implement/tally.test.js @@ -23,12 +23,21 @@ 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.todo("tally on an empty array returns an empty object"); +test("returns an empty object when array is empty", () => { + expect(tally([])).toEqual({}); +}); // Given an array with duplicate items // When passed to tally // Then it should return counts for each unique item +test("returns counts for each unique item when an array with duplicate items", () => { + 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("throw an error when input not an array", () => { + expect(() => tally('a', 'a', 'b', 'c')).toThrow(Error); +}); From 7e7040912c56682d3fa77d7ab3561b0f369eed07 Mon Sep 17 00:00:00 2001 From: Elisabeth-Matulian Date: Fri, 6 Mar 2026 19:06:42 +0000 Subject: [PATCH 3/3] complete-interpret-exercises --- Sprint-2/interpret/invert.js | 12 +++++++++++- Sprint-2/interpret/invert.test.js | 6 ++++++ 2 files changed, 17 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..88885deae 100644 --- a/Sprint-2/interpret/invert.js +++ b/Sprint-2/interpret/invert.js @@ -10,20 +10,30 @@ 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 } +// invertedObj.key = value; this line creates a key literally with name "key". it returns {key : 1} // b) What is the current return value when invert is called with { a: 1, b: 2 } +// every time it meets the key and reassign it, so {key : 2} // c) What is the target return value when invert is called with {a : 1, b: 2} +// to swap pair key-value with each other. should return {1 : a, 2: b} // c) What does Object.entries return? Why is it needed in this program? +// it returns key-value pairs in arrays nested of array. we need Object.entries to convert key-value pairs to array because for..of doesn't work with objects. +//I wonder if we could solve the function by swapping key-value using [key, value] = [value, key] in some way? p.s. I'll delete this comment after review:) // d) Explain why the current return value is different from the target output +// invertedObj.key = value; this line creates a key literally with name "key". it returns {key : 1} // e) Fix the implementation of invert (and write tests to prove it's fixed!) + + +module.exports = invert; \ No newline at end of file diff --git a/Sprint-2/interpret/invert.test.js b/Sprint-2/interpret/invert.test.js new file mode 100644 index 000000000..a1c55d61e --- /dev/null +++ b/Sprint-2/interpret/invert.test.js @@ -0,0 +1,6 @@ +const invert = require("./invert.js"); + + +test(" should return an object with swapped keys and values", () => { + expect(invert({"a" : "1", "b": "2"})).toEqual({"1" : "a", "2" : "b"}); +}); \ No newline at end of file