diff --git a/Sprint-2/debug/address.js b/Sprint-2/debug/address.js index 940a6af83..fde180d6d 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..a413b3332 100644 --- a/Sprint-2/debug/recipe.js +++ b/Sprint-2/debug/recipe.js @@ -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..cf2d0757a 100644 --- a/Sprint-2/implement/contains.js +++ b/Sprint-2/implement/contains.js @@ -1,3 +1,5 @@ -function contains() {} +function contains(obj, property_name) { + return property_name in obj; +} module.exports = contains; diff --git a/Sprint-2/implement/contains.test.js b/Sprint-2/implement/contains.test.js index 326bdb1f2..aa1f35ebe 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")).toBe(false); +}); // Given an object with properties // When passed to contains with an existing property name // Then it should return true +test("returns true when object contains an existing property", () => { + 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 when object does not contain the property", () => { + 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("returns false or throws an error when passed invalid parameters like an array", () => { + expect(() => contains([], "a")).not.toThrow(); + expect(contains([], "a")).toBe(false); +}); diff --git a/Sprint-2/implement/lookup.js b/Sprint-2/implement/lookup.js index a6746e07f..4373213ac 100644 --- a/Sprint-2/implement/lookup.js +++ b/Sprint-2/implement/lookup.js @@ -1,5 +1,11 @@ -function createLookup() { +function createLookup(country_currency_pairs) { // implementation here + const lookup = {}; + for (const [country, currency] of country_currency_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..beb33b5b3 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 @@ -33,3 +31,37 @@ It should return: 'CA': 'CAD' } */ + +test("creates a country currency code lookup for multiple codes", () => { + const countryCurrencyPairs = [ + ["US", "USD"], + ["CA", "CAD"], + ]; + + const result = createLookup(countryCurrencyPairs); + + expect(result).toEqual({ + US: "USD", + CA: "CAD", + }); +}); + +test("creates a country currency code lookup for a single code", () => { + const countryCurrencyPairs = [ + ["GB", "GBP"], + ]; + + const result = createLookup(countryCurrencyPairs); + + expect(result).toEqual({ + GB: "GBP", + }); +}); + +test("returns an empty object when given an empty array", () => { + const countryCurrencyPairs = []; + + const result = createLookup(countryCurrencyPairs); + + expect(result).toEqual({}); +}); diff --git a/Sprint-2/implement/querystring.js b/Sprint-2/implement/querystring.js index 45ec4e5f3..c825d8cef 100644 --- a/Sprint-2/implement/querystring.js +++ b/Sprint-2/implement/querystring.js @@ -1,16 +1,25 @@ function parseQueryString(queryString) { - const queryParams = {}; - if (queryString.length === 0) { - return queryParams; - } - const keyValuePairs = queryString.split("&"); + if (queryString === "") { + return {}; + } else { + queryString = decodeURIComponent(queryString) + .replace(/\+/g, " ") + .replace(/&&/g, "&") + .replace(/&+$/, ""); - for (const pair of keyValuePairs) { - const [key, value] = pair.split("="); - queryParams[key] = value; - } + const splitByAnds = queryString.split("&"); + + const splitByEquals = splitByAnds.map(x => { + const match = x.match(/^([^=]*)(?:=(.*))?$/); - return queryParams; + const key = match[1]; + const value = match[2] ?? ""; + + return [key, value]; + }); + + return Object.fromEntries(splitByEquals); + } } module.exports = parseQueryString; diff --git a/Sprint-2/implement/querystring.test.js b/Sprint-2/implement/querystring.test.js index 328b8df61..909f04abc 100644 --- a/Sprint-2/implement/querystring.test.js +++ b/Sprint-2/implement/querystring.test.js @@ -37,12 +37,62 @@ test("should replace '+' by ' '", () => { }); }); -// Stretch exercise: Handling query strings that contain identical keys +test("should parse basic key-value pairs", () => { + expect(parseQueryString("name=John&age=30")).toEqual({ + name: "John", + age: "30", + }); +}); + +test("should replace '+' by ' '", () => { + expect(parseQueryString("name=John+Doe&city=New+York")).toEqual({ + name: "John Doe", + city: "New York", + }); + + expect(parseQueryString("full+name=John+Doe")).toEqual({ + "full name": "John Doe", + }); +}); + +test("should decode percent-encoded characters", () => { + expect(parseQueryString("message=Hello%20World")).toEqual({ + message: "Hello World", + }); -// Delete this test if you are not working on this optional case -test("should store values of a key in an array when the key has 2 or more values", () => { - expect(parseQueryString("key=value1&key=value2&key=value3&foo=bar")).toEqual({ - key: ["value1", "value2", "value3"], - foo: "bar", + expect(parseQueryString("%24half=1%2F2")).toEqual({ + $half: "1/2", }); }); + +test("should handle keys without values", () => { + expect(parseQueryString("debug")).toEqual({ + debug: "", + }); + + expect(parseQueryString("key")).toEqual({ + key: "", + }); +}); + +test("should handle empty values", () => { + expect(parseQueryString("name=&age=25")).toEqual({ + name: "", + age: "25", + }); + + expect(parseQueryString("key=")).toEqual({ + key: "", + }); +}); + +test("should return empty object for empty input", () => { + expect(parseQueryString("")).toEqual({}); +}); + +test("should use the last value when duplicate keys exist", () => { + expect(parseQueryString("color=red&color=blue")).toEqual({ + color: "blue", + }); +}); + diff --git a/Sprint-2/implement/tally.js b/Sprint-2/implement/tally.js index f47321812..84b03e706 100644 --- a/Sprint-2/implement/tally.js +++ b/Sprint-2/implement/tally.js @@ -1,3 +1,16 @@ -function tally() {} +function tally(items) { + if (!Array.isArray(items)) { + throw new TypeError("Input must be an array"); + } + + const uniqueItems = [...new Set(items)].sort(); + + const counts = {}; + for (const item of uniqueItems) { + counts[item] = items.filter(x => x === item).length; + } + + return counts; +} module.exports = tally; diff --git a/Sprint-2/implement/tally.test.js b/Sprint-2/implement/tally.test.js index 2ceffa8dd..ea349000e 100644 --- a/Sprint-2/implement/tally.test.js +++ b/Sprint-2/implement/tally.test.js @@ -20,15 +20,36 @@ const tally = require("./tally.js"); // When passed an array of items // Then it should return an object containing the count for each unique item +test("tally counts the frequency of each unique item", () => { + expect(tally(["a", "a", "b", "c"])).toEqual({ + a: 2, + b: 1, + c: 1, + }); +}); + // 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", "a"])).toEqual({ + a: 3, + }); +}); + // Given an invalid input like a string // When passed to tally // Then it should throw an error + +test("tally throws an error when passed invalid input", () => { + expect(() => tally("a")).toThrow(); +}); diff --git a/Sprint-2/interpret/invert.js b/Sprint-2/interpret/invert.js index bb353fb1f..f04b2fb08 100644 --- a/Sprint-2/interpret/invert.js +++ b/Sprint-2/interpret/invert.js @@ -9,8 +9,8 @@ function invert(obj) { const invertedObj = {}; - for (const [key, value] of Object.entries(obj)) { - invertedObj.key = value; + for (const [value, key] of Object.entries(obj)) { + invertedObj[key] = value; } return invertedObj; diff --git a/Sprint-2/interpret/invert.test.js b/Sprint-2/interpret/invert.test.js new file mode 100644 index 000000000..fd92c1baf --- /dev/null +++ b/Sprint-2/interpret/invert.test.js @@ -0,0 +1,46 @@ +const invert = require("./invert.js"); + +/* +Implement a function called invert that swaps the keys and values of an object. + +E.g. invert({ x: 10, y: 20 }) +// returns { "10": "x", "20": "y" } +*/ + +// Acceptance criteria: + +// Given an empty object +// When passed to invert +// Then it should return an empty object +test("returns an empty object when given an empty object", () => { + expect(invert({})).toEqual({}); +}); + +// Given an object with one property +// When passed to invert +// Then it should swap the key and value +test("inverts an object with one key-value pair", () => { + expect(invert({ a: 1 })).toEqual({ + "1": "a", + }); +}); + +// Given an object with multiple properties +// When passed to invert +// Then it should swap all keys and values +test("inverts an object with multiple key-value pairs", () => { + expect(invert({ a: 1, b: 2 })).toEqual({ + "1": "a", + "2": "b", + }); +}); + +// Given an object with string values +// When passed to invert +// Then it should swap the keys and values +test("inverts string values correctly", () => { + expect(invert({ first: "apple", second: "banana" })).toEqual({ + apple: "first", + banana: "second", + }); +});