diff --git a/Sprint-2/debug/address.js b/Sprint-2/debug/address.js index 940a6af83..f762a7ef7 100644 --- a/Sprint-2/debug/address.js +++ b/Sprint-2/debug/address.js @@ -4,6 +4,8 @@ // but it isn't working... // Fix anything that isn't working +// I Think the issue is the console.log statement. I will fix it buy using dot notation to access the houseNumber property of the address object. +// It is trying to access the houseNumber using an index, but it should use the property houseNumber. const address = { houseNumber: 42, street: "Imaginary Road", @@ -12,4 +14,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..c983e37c6 100644 --- a/Sprint-2/debug/author.js +++ b/Sprint-2/debug/author.js @@ -1,4 +1,4 @@ -// Predict and explain first... +// Predict and explain first... It won't work because the method `for...of` is used for iterable objects like arrays. // 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 +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..1bc56b1f1 100644 --- a/Sprint-2/debug/recipe.js +++ b/Sprint-2/debug/recipe.js @@ -1,4 +1,4 @@ -// Predict and explain first... +// Predict and explain first... it won't work because the ingredients are not being accessed correctly. // This program should log out the title, how many it serves and the ingredients. // Each ingredient should be logged on a new line @@ -11,5 +11,7 @@ const recipe = { }; console.log(`${recipe.title} serves ${recipe.serves} - ingredients: -${recipe}`); +ingredients:`); +recipe.ingredients.forEach((ingredient) => { + console.log(ingredient); +}); \ No newline at end of file diff --git a/Sprint-2/implement/contains.js b/Sprint-2/implement/contains.js index cd779308a..090162109 100644 --- a/Sprint-2/implement/contains.js +++ b/Sprint-2/implement/contains.js @@ -1,3 +1,9 @@ -function contains() {} +function contains(obj, key) { + if (typeof obj !== "object" || obj === null || Array.isArray(obj)) { + return false; + } + return Object.prototype.hasOwnProperty.call(obj, key); + +} module.exports = contains; diff --git a/Sprint-2/implement/contains.test.js b/Sprint-2/implement/contains.test.js index 326bdb1f2..ccec0ad75 100644 --- a/Sprint-2/implement/contains.test.js +++ b/Sprint-2/implement/contains.test.js @@ -16,20 +16,58 @@ 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("contains returns true for existing property", () => { + const currentOutput = contains({ k: 13, b: 5 }, 'k'); + const targetOutput = true; + + expect(currentOutput).toEqual(targetOutput); +}); +test("contains returns false for non-existent property", () => { + const currentOutput = contains({ k: 13, b: 5 }, 'x'); + const targetOutput = false; + + expect(currentOutput).toEqual(targetOutput); +}); // 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", () => { + const currentOutput = contains({}, 'anyProperty'); + const targetOutput = false; + expect(currentOutput).toEqual(targetOutput); +}); // Given an object with properties // When passed to contains with an existing property name // Then it should return true +test("contains returns true for existing property in object", () => { + const currentOutput = contains({ name: "Alice", age: 30 }, 'name'); + const targetOutput = true; + expect(currentOutput).toEqual(targetOutput); +}); // Given an object with properties // When passed to contains with a non-existent property name // Then it should return false +test("contains returns false for non-existent property in object", () => { + const currentOutput = contains({ name: "Alice", age: 30 }, 'address'); + const targetOutput = false; + expect(currentOutput).toEqual(targetOutput); +}); // Given invalid parameters like an array // When passed to contains // Then it should return false or throw an error +test("contains returns false for array input", () => { + const currentOutput = contains([1, 2, 3], 'length'); + const targetOutput = false; // Arrays are objects, but 'length' is not a property in this context + expect(currentOutput).toEqual(targetOutput); +}); + +// Given an object with nested properties +test("contains returns the correct value for nested properties", () => { + const currentOutput = contains ({ person: { name: "Bob", age: 25 } }, 'person'); + const targetOutput = true; + expect(currentOutput).toEqual(targetOutput); +}); diff --git a/Sprint-2/implement/lookup.js b/Sprint-2/implement/lookup.js index a6746e07f..f7c3e1e47 100644 --- a/Sprint-2/implement/lookup.js +++ b/Sprint-2/implement/lookup.js @@ -1,5 +1,11 @@ -function createLookup() { - // implementation here +function createLookup(pairs) { + const result = {}; + + for (const [countryCode, currencyCode] of pairs) { + result[countryCode] = currencyCode; + } + + return result; } module.exports = createLookup; diff --git a/Sprint-2/implement/lookup.test.js b/Sprint-2/implement/lookup.test.js index 547e06c5a..c7e97e809 100644 --- a/Sprint-2/implement/lookup.test.js +++ b/Sprint-2/implement/lookup.test.js @@ -1,7 +1,5 @@ const createLookup = require("./lookup.js"); -test.todo("creates a country currency code lookup for multiple codes"); - /* Create a lookup object of key value pairs from an array of code pairs @@ -23,6 +21,7 @@ Then Example Given: [['US', 'USD'], ['CA', 'CAD']] + When createLookup(countryCurrencyPairs) is called @@ -33,3 +32,23 @@ It should return: 'CA': 'CAD' } */ + + + + test('returns correct lookup object for valid input', () => { + const input = [['US', 'USD'], ['CA', 'CAD']]; + const expected = { US: 'USD', CA: 'CAD' }; + expect(createLookup(input)).toEqual(expected); + }); + + test('handles mixed data correctly', () => { + const input = [['JP', 'JPY'], ['FR', 'EUR'], ['GB', 'GBP']]; + const expected = { JP: 'JPY', FR: 'EUR', GB: 'GBP' }; + expect(createLookup(input)).toEqual(expected); + }); + + test('returns empty object for empty input', () => { + const input = []; + const expected = {}; + expect(createLookup(input)).toEqual(expected); + }); \ No newline at end of file diff --git a/Sprint-2/implement/querystring.js b/Sprint-2/implement/querystring.js index 45ec4e5f3..578ae0a7c 100644 --- a/Sprint-2/implement/querystring.js +++ b/Sprint-2/implement/querystring.js @@ -6,8 +6,8 @@ function parseQueryString(queryString) { const keyValuePairs = queryString.split("&"); for (const pair of keyValuePairs) { - const [key, value] = pair.split("="); - queryParams[key] = value; + const [key, ...rest] = pair.split("="); + queryParams[key] = rest.join("="); } return queryParams; diff --git a/Sprint-2/implement/querystring.test.js b/Sprint-2/implement/querystring.test.js index 3e218b789..b1bc3e94e 100644 --- a/Sprint-2/implement/querystring.test.js +++ b/Sprint-2/implement/querystring.test.js @@ -10,3 +10,21 @@ test("parses querystring values containing =", () => { "equation": "x=y+1", }); }); + +test("parses querystring with multiple values", () => { + expect(parseQueryString("name=John&age=30")).toEqual({ + "name": "John", + "age": "30", + }); +}); +test("parses querystring with multiple values with same key", () => { + expect(parseQueryString("name=John&name=Jane")).toEqual({ + "name": "Jane", // Last value should overwrite previous ones + }); +}); + +test("parses querystring without key", () => { + expect(parseQueryString("=Python")).toEqual({ + "": "Python", // Empty key should be handled + }); +}); \ No newline at end of file diff --git a/Sprint-2/implement/tally.js b/Sprint-2/implement/tally.js index f47321812..3e53d07ce 100644 --- a/Sprint-2/implement/tally.js +++ b/Sprint-2/implement/tally.js @@ -1,3 +1,18 @@ -function tally() {} +function tally(array) { + if (!Array.isArray(array)) { + throw new Error("Input must be an array"); + } + const counts = {}; + + for (const item of array) { + if (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 2ceffa8dd..b595ed323 100644 --- a/Sprint-2/implement/tally.test.js +++ b/Sprint-2/implement/tally.test.js @@ -23,12 +23,24 @@ 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", () => { + const currentOutput = tally([]); + const targetOutput = {}; + expect(currentOutput).toEqual(targetOutput); +}); // Given an array with duplicate items // When passed to tally // Then it should return counts for each unique item +test("tally counts unique items in an array", () => { + const currentOutput = tally(['a', 'b', 'a', 'c', 'b']); + const targetOutput = { a: 2, b: 2, c: 1 }; + expect(currentOutput).toEqual(targetOutput); +}); // 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("invalid input")).toThrow("Input must be an array"); + }); \ No newline at end of file diff --git a/Sprint-2/interpret/invert.js b/Sprint-2/interpret/invert.js index bb353fb1f..7d8332702 100644 --- a/Sprint-2/interpret/invert.js +++ b/Sprint-2/interpret/invert.js @@ -10,20 +10,35 @@ function invert(obj) { const invertedObj = {}; for (const [key, value] of Object.entries(obj)) { - invertedObj.key = value; + invertedObj[value] = key; } - return invertedObj; + return invertedObj; } -// a) What is the current return value when invert is called with { a : 1 } +// 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 returns an array of key-value pairs from the object. It is needed to iterate over both keys and values in the object. // d) Explain why the current return value is different from the target output - +// The current return value is incorrect because the code is assigning the value to the key instead of the key to the value in the inverted object. // e) Fix the implementation of invert (and write tests to prove it's fixed!) + +test("invert swaps keys and values in an object", () => { + const currentOutput = invert({ x: 10, y: 20 }); + const targetOutput = { "10": "x", "20": "y" }; + + expect(currentOutput).toEqual(targetOutput); +}); + +test("invert handles multiple key-value pairs", () => { + const currentOutput = invert({ a: 1, b: 2, c: 3 }); + const targetOutput = { "1": "a", "2": "b", "3": "c" }; + + expect(currentOutput).toEqual(targetOutput); +}); \ No newline at end of file diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 000000000..55cde53eb --- /dev/null +++ b/package-lock.json @@ -0,0 +1,6 @@ +{ + "name": "Module-Data-Groups", + "lockfileVersion": 3, + "requires": true, + "packages": {} +}