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}`); 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); } diff --git a/Sprint-2/debug/recipe.js b/Sprint-2/debug/recipe.js index 6cbdd22cd..7b93b17b8 100644 --- a/Sprint-2/debug/recipe.js +++ b/Sprint-2/debug/recipe.js @@ -11,5 +11,5 @@ const recipe = { }; console.log(`${recipe.title} serves ${recipe.serves} - ingredients: -${recipe}`); +ingredients: +${recipe.ingredients.join("\n")}`); diff --git a/Sprint-2/implement/contains.js b/Sprint-2/implement/contains.js index cd779308a..40f559954 100644 --- a/Sprint-2/implement/contains.js +++ b/Sprint-2/implement/contains.js @@ -1,3 +1,10 @@ -function contains() {} +function contains(obj, prop) { + if (typeof obj !== "object" || obj === null || Array.isArray(obj)) { + return false; + } -module.exports = contains; + // Use the static Object.hasOwn() method + return Object.hasOwn(obj, prop); +} + +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..7d55a7581 100644 --- a/Sprint-2/implement/contains.test.js +++ b/Sprint-2/implement/contains.test.js @@ -20,16 +20,28 @@ as the object doesn't contains a key of 'c' // Given an empty object // When passed to contains // Then it should return false -test.todo("contains on empty object returns false"); +test("contains on empty object returns false", () => { + expect(contains({}, "a")).toEqual(false); +}); // Given an object with properties // When passed to contains with an existing property name // Then it should return true +test("contains an object with propery exist, returns true", () => { + expect(contains({ a: 1, b: 2 }, "a")).toEqual(true); +}); // Given an object with properties // When passed to contains with a non-existent property name // Then it should return false - +test("return false when the property does not exist", () => { + expect(contains({ a: 1, b: 3 }, "c")).toEqual(false); +}); // Given invalid parameters like an array // When passed to contains // Then it should return false or throw an error +test("returns false when input is an array, even if the index exists", () => { + // Index 0 exists in this array, but contains() should return false + // because the requirement is for plain objects only. + expect(contains(["apple", "banana"], 0)).toEqual(false); +}); diff --git a/Sprint-2/implement/lookup.js b/Sprint-2/implement/lookup.js index a6746e07f..f8253d2c4 100644 --- a/Sprint-2/implement/lookup.js +++ b/Sprint-2/implement/lookup.js @@ -1,5 +1,13 @@ -function createLookup() { - // implementation here +function createLookup(countryCurrencyPairs) { + const lookup = {}; + + for (const [countryCode, countryCurrency] of countryCurrencyPairs) { + lookup[countryCode] = countryCurrency; + } + + return lookup; } module.exports = createLookup; + + diff --git a/Sprint-2/implement/lookup.test.js b/Sprint-2/implement/lookup.test.js index 547e06c5a..b9538744e 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" + + }) + +}) /* diff --git a/Sprint-2/implement/querystring.js b/Sprint-2/implement/querystring.js index 45ec4e5f3..8a750be1f 100644 --- a/Sprint-2/implement/querystring.js +++ b/Sprint-2/implement/querystring.js @@ -1,13 +1,22 @@ function parseQueryString(queryString) { + if (queryString.startsWith("?")) { + queryString = queryString.substring(1); + } + const queryParams = {}; 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 [key, ...valueParts] = pair.split("="); + const value = valueParts.join("="); + + queryParams[decodeURIComponent(key)] = decodeURIComponent(value); } return queryParams; diff --git a/Sprint-2/implement/tally.js b/Sprint-2/implement/tally.js index f47321812..3f649133b 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 Error("Input must be an array"); + } + + const result = Object.create(null); + + for (const item of arr) { + if (result[item] === undefined) { + result[item] = 1; + } else { + result[item] = result[item] + 1; + } + } + + return result; +} module.exports = tally; diff --git a/Sprint-2/implement/tally.test.js b/Sprint-2/implement/tally.test.js index 2ceffa8dd..0c19cd2b9 100644 --- a/Sprint-2/implement/tally.test.js +++ b/Sprint-2/implement/tally.test.js @@ -23,12 +23,45 @@ 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 counts duplicate items correctly", () => { + 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("tally throws an error for invalid input", () => { + expect(() => tally("hello")).toThrow(); +}); + +// Given an array containing keys that match built-in object properties +// When passed to tally +// Then it should correctly count them without conflicts from inherited properties +test("tally works correctly with reserved words like toString", () => { + const input = ["toString", "toString", "hasOwnProperty"]; + const expected = { + toString: 2, + hasOwnProperty: 1, + }; + expect(tally(input)).toEqual(expected); +}); + +// Extra verification test to confirm pure object behavior +test("tally accurately counts prototype method names as strings", () => { + const input = ["toString", "toString"]; + const expected = { + toString: 2, + }; + expect(tally(input)).toEqual(expected); +}); diff --git a/Sprint-2/interpret/invert.js b/Sprint-2/interpret/invert.js index bb353fb1f..88da66dd9 100644 --- a/Sprint-2/interpret/invert.js +++ b/Sprint-2/interpret/invert.js @@ -1,29 +1,50 @@ -// Let's define how invert should work +// 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" } -// Given an object -// When invert is passed this object -// Then it should swap the keys and values in the object +// d) What does Object.entries return? Why is it needed in this program? +// It returns an array of [key, value] pairs. +// It is needed so we can loop through both keys and values at the same time when inverting the object. -// E.g. invert({x : 10, y : 20}), target output: {"10": "x", "20": "y"} +// e) Explain why the current return value is different from the target output +// The original code used 'invertedObj.key = value', which creates a property literally named "key" +// instead of using the value dynamically. +// This causes each iteration to overwrite the previous one. +// It also does not handle collisions (multiple keys with the same value). +// f) Fix the implementation of invert (and write tests to prove it's fixed!) function invert(obj) { const invertedObj = {}; for (const [key, value] of Object.entries(obj)) { - invertedObj.key = value; + if (invertedObj[value] === undefined) { + invertedObj[value] = key; + } else if (Array.isArray(invertedObj[value])) { + invertedObj[value].push(key); + } else { + invertedObj[value] = [invertedObj[value], key]; + } } return invertedObj; } -// a) What is the current return value when invert is called with { a : 1 } - -// b) What is the current return value when invert is called with { a: 1, b: 2 } +// --- CONSOLE TESTS TO PROVE IT IS FIXED --- +console.log("Test 1 (Simple):", invert({ a: 1 })); +// Expected: { '1': 'a' } -// c) What is the target return value when invert is called with {a : 1, b: 2} +console.log("Test 2 (No duplicates):", invert({ x: 10, y: 20 })); +// Expected: { '10': 'x', '20': 'y' } -// c) What does Object.entries return? Why is it needed in this program? +console.log("Test 3 (Collision):", invert({ a: 1, b: 1 })); +// Expected: { '1': ['a', 'b'] } -// d) Explain why the current return value is different from the target output +console.log("Test 4 (Triple collision):", invert({ a: 1, b: 1, c: 1 })); +// Expected: { '1': ['a', 'b', 'c'] } -// e) Fix the implementation of invert (and write tests to prove it's fixed!) +module.exports = invert; \ No newline at end of file