diff --git a/Sprint-2/debug/address.js b/Sprint-2/debug/address.js index 940a6af83..ccac56018 100644 --- a/Sprint-2/debug/address.js +++ b/Sprint-2/debug/address.js @@ -1,5 +1,5 @@ // Predict and explain first... - +//This code is using the wrong syntax to access the houseNumber property of the address object. It will therefore return undefined. // This code should log out the houseNumber from the address object // but it isn't working... // Fix anything that isn't working @@ -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..40b981618 100644 --- a/Sprint-2/debug/author.js +++ b/Sprint-2/debug/author.js @@ -1,5 +1,5 @@ // Predict and explain first... - +// This program attempts to log out all the property values in the object. its not going to work because we cannot use "for of" loop in this situation. // 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) { - console.log(value); +for (const key in author) { + console.log(author[key]); } diff --git a/Sprint-2/debug/recipe.js b/Sprint-2/debug/recipe.js index 6cbdd22cd..e15b83f35 100644 --- a/Sprint-2/debug/recipe.js +++ b/Sprint-2/debug/recipe.js @@ -1,5 +1,5 @@ // Predict and explain first... - +// This program will not log out the ingredients correctly because we are trying to log the whole recipe object instead of the ingredients array. // 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? @@ -12,4 +12,4 @@ const recipe = { console.log(`${recipe.title} serves ${recipe.serves} ingredients: -${recipe}`); +${recipe.ingredients.join("\n")}`); diff --git a/Sprint-2/implement/contains.js b/Sprint-2/implement/contains.js index cd779308a..758f6667f 100644 --- a/Sprint-2/implement/contains.js +++ b/Sprint-2/implement/contains.js @@ -1,3 +1,8 @@ -function contains() {} +function contains(obj, key) { + if (typeof obj !== 'object' || obj === null || Array.isArray(obj)) { + return false; + } + return Object.hasOwn(obj, key); +} module.exports = contains; diff --git a/Sprint-2/implement/contains.test.js b/Sprint-2/implement/contains.test.js index 326bdb1f2..eec1701f8 100644 --- a/Sprint-2/implement/contains.test.js +++ b/Sprint-2/implement/contains.test.js @@ -16,7 +16,8 @@ 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 returns true for existing property"); +test.todo("contains returns false for non-existent property"); // Given an empty object // When passed to contains // Then it should return false @@ -25,11 +26,12 @@ test.todo("contains on empty object returns false"); // Given an object with properties // When passed to contains with an existing property name // Then it should return true - +test.todo("contains returns true for existing property"); // Given an object with properties // When passed to contains with a non-existent property name // Then it should return false - +test.todo("contains returns false for non-existent property"); // Given invalid parameters like an array // When passed to contains // Then it should return false or throw an error +test.todo("contains with invalid parameters returns false or throws error"); \ No newline at end of file diff --git a/Sprint-2/implement/lookup.js b/Sprint-2/implement/lookup.js index a6746e07f..0cb505fe7 100644 --- a/Sprint-2/implement/lookup.js +++ b/Sprint-2/implement/lookup.js @@ -1,5 +1,10 @@ -function createLookup() { +function createLookup(pairs) { // implementation here + const lookup = {}; + for (const [country, currency] of pairs) { + lookup[country] = currency; + } + return lookup; } module.exports = createLookup; diff --git a/Sprint-2/implement/lookup.test.js b/Sprint-2/implement/lookup.test.js index 547e06c5a..a35173d6a 100644 --- a/Sprint-2/implement/lookup.test.js +++ b/Sprint-2/implement/lookup.test.js @@ -1,6 +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 an empty array"); +test.todo("creates a country currency code lookup for duplicate country codes"); +test.todo("creates a country currency code lookup for invalid input"); /* diff --git a/Sprint-2/implement/querystring.js b/Sprint-2/implement/querystring.js index 45ec4e5f3..cdd625ae1 100644 --- a/Sprint-2/implement/querystring.js +++ b/Sprint-2/implement/querystring.js @@ -6,10 +6,15 @@ function parseQueryString(queryString) { const keyValuePairs = queryString.split("&"); for (const pair of keyValuePairs) { - const [key, value] = pair.split("="); - queryParams[key] = value; + const index = pair.indexOf("="); + if (index === -1) { + queryParams[pair] = ""; + } else { + const key = pair.substring(0, index); + const value = pair.substring(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..ecba3f6cb 100644 --- a/Sprint-2/implement/querystring.test.js +++ b/Sprint-2/implement/querystring.test.js @@ -10,3 +10,22 @@ test("parses querystring values containing =", () => { "equation": "x=y+1", }); }); +test("parses multiple parameters", () => { + expect(parseQueryString("a=1&b=2")).toEqual({ a: "1", b: "2" }); +}); +test("parses empty querystring", () => { + expect(parseQueryString("")).toEqual({}); +}); +test("parses parameter without value", () => { + expect(parseQueryString("flag")).toEqual({ flag: "" }); +}); +test("parses parameter with empty value", () => { + expect(parseQueryString("empty=")).toEqual({ empty: "" }); +}); +test("parses parameter with multiple =", () => { + expect(parseQueryString("data=a=b=c")).toEqual({ data: "a=b=c" }); +}); +test("parses parameter with special characters", () => { + expect(parseQueryString("name=John%20Doe&age=30")).toEqual({ name: "John%20Doe", age: "30" }); +}); + diff --git a/Sprint-2/implement/tally.js b/Sprint-2/implement/tally.js index f47321812..85bb80c7f 100644 --- a/Sprint-2/implement/tally.js +++ b/Sprint-2/implement/tally.js @@ -1,3 +1,16 @@ -function tally() {} +function tally(arr) { + if (!Array.isArray(arr)) { + throw new Error("Input must be an array"); + } + const counts = Object.create(null); + for (const item of arr) { + if (counts[item] === undefined) { + counts[item] = 1; + } else { + counts[item] = 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..80c64442c 100644 --- a/Sprint-2/implement/tally.test.js +++ b/Sprint-2/implement/tally.test.js @@ -19,6 +19,7 @@ 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.todo("tally returns correct counts for unique items in an array"); // Given an empty array // When passed to tally @@ -28,7 +29,9 @@ test.todo("tally on an empty array returns an empty object"); // Given an array with duplicate items // When passed to tally // Then it should return counts for each unique item +test.todo("tally returns correct counts for duplicate items in an array"); // Given an invalid input like a string // When passed to tally // Then it should throw an error +test.todo("tally with invalid input throws an error"); diff --git a/Sprint-2/interpret/invert.js b/Sprint-2/interpret/invert.js index bb353fb1f..1379efb14 100644 --- a/Sprint-2/interpret/invert.js +++ b/Sprint-2/interpret/invert.js @@ -17,13 +17,43 @@ function invert(obj) { } // 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"} // c) What does Object.entries return? Why is it needed in this program? - +//It returns an array of [key, value] pairs from the object. It is needed so we can loop over each key/value pair. // d) Explain why the current return value is different from the target output - +//The .key is incorrect syntax for setting a key in the object. It should be in brackets. // e) 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[value] = key; + } + + return invertedObj; +} + +module.exports = invert; + +//jest test cases +const invert = require("./invert.js"); + +test("inverts a single object", () => { + expect(invert({ a: 1 })).toEqual({ "1": "a" }); +}); + +test("inverts an object with multiple key-value pairs", () => { + expect(invert({ a: 1, b: 2 })).toEqual({ "1": "a", "2": "b" }); +}); + +test("inverts string values", () => { + expect(invert({ a: "hello", b: "world" })).toEqual({ "hello": "a", "world": "b" }); +}); + +test("inverts an empty object", () => { + expect(invert({})).toEqual({}); +}); \ No newline at end of file