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..81e8e7fab 100644 --- a/Sprint-2/implement/contains.js +++ b/Sprint-2/implement/contains.js @@ -1,3 +1,9 @@ -function contains() {} +function contains(obj, prop) { + if (typeof obj !== "object" || obj === null || Array.isArray(obj)) { + return false; + } + + return prop in obj; +} module.exports = contains; diff --git a/Sprint-2/implement/contains.test.js b/Sprint-2/implement/contains.test.js index 326bdb1f2..26f1d8d4c 100644 --- a/Sprint-2/implement/contains.test.js +++ b/Sprint-2/implement/contains.test.js @@ -20,16 +20,27 @@ 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("return false when paramters invaliud ", () =>{ + expect(contains([], "a")).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..7453d4cb3 100644 --- a/Sprint-2/implement/querystring.js +++ b/Sprint-2/implement/querystring.js @@ -1,13 +1,18 @@ 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; + const [key, ...valueParts] = pair.split("="); + queryParams[key] = valueParts.join("="); } return queryParams; diff --git a/Sprint-2/implement/tally.js b/Sprint-2/implement/tally.js index f47321812..1f3b66638 100644 --- a/Sprint-2/implement/tally.js +++ b/Sprint-2/implement/tally.js @@ -1,3 +1,26 @@ -function tally() {} +function tally(arr) { + // invalid input → throw error + if (!Array.isArray(arr)) { + throw new Error("Input must be an array"); + } -module.exports = tally; + // empty array → return empty object + if (arr.length === 0) { + return {}; + } + + const result = {}; + + // count items + for (const item of arr) { + if (result[item] === undefined) { + result[item] = 1; + } else { + result[item] = result[item] + 1; + } + } + + return result; +} + +module.exports = tally; \ No newline at end of file diff --git a/Sprint-2/implement/tally.test.js b/Sprint-2/implement/tally.test.js index 2ceffa8dd..8632463f3 100644 --- a/Sprint-2/implement/tally.test.js +++ b/Sprint-2/implement/tally.test.js @@ -18,17 +18,31 @@ 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 +// Then it should return an object containing the count for each unique ite // 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(); +}); \ No newline at end of file diff --git a/Sprint-2/interpret/invert.js b/Sprint-2/interpret/invert.js index bb353fb1f..8fd6f05f0 100644 --- a/Sprint-2/interpret/invert.js +++ b/Sprint-2/interpret/invert.js @@ -5,25 +5,63 @@ // Then it should swap the keys and values in the object // E.g. invert({x : 10, y : 20}), target output: {"10": "x", "20": "y"} - 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 } - +//{"1": "a"}. // b) What is the current return value when invert is called with { a: 1, b: 2 } - +//{"1": "a", "2": "b"} // 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? +// Object.entries(obj) returns an array of [key, value] pairs, where each pair is stored as an array. +// It is needed in this program so we can loop through both the keys and values of the object at the same time when inverting it. +//it returns an array of arrays, where each inner array is a pair: [key, value]. +//The current return value is different from the target output because when multiple keys have +// the same value, they overwrite each other when inverted. This causes some values to be lost. +// e) Fix the implementation of invert (and write tests to prove it's fixed!) +function invert(obj) { + const invertedObj = {}; -// d) Explain why the current return value is different from the target output + for (const [key, value] of Object.entries(obj)) { + // Check if the value already exists as a key in our new object + if (invertedObj[value] === undefined) { + // First time seeing this: Save it as a string + invertedObj[value] = key; + } else if (Array.isArray(invertedObj[value])) { + // It's already an array: Add the new key to the list + invertedObj[value].push(key); + } else { + // It's a string: Turn it into an array and add the new key + invertedObj[value] = [invertedObj[value], key]; + } + } -// e) Fix the implementation of invert (and write tests to prove it's fixed!) + return invertedObj; +} + +// --- CONSOLE TESTS --- +console.log("Test 1 (Simple):", invert({ a: 1 })); +// Expected: { "1": "a" } + +console.log("Test 2 (No duplicates):", invert({ x: 10, y: 20 })); +// Expected: { "10": "x", "20": "y" } + +console.log("Test 3 (Collision):", invert({ a: 1, b: 1 })); +// Expected: { "1": ["a", "b"] } + +console.log("Test 4 (Triple collision):", invert({ a: 1, b: 1, c: 1 })); +// Expected: { "1": ["a", "b", "c"] }