diff --git a/Sprint-2/debug/address.js b/Sprint-2/debug/address.js index 940a6af83..4f2fa6614 100644 --- a/Sprint-2/debug/address.js +++ b/Sprint-2/debug/address.js @@ -1,5 +1,7 @@ // Predict and explain first... +// Trying to access the address object using array notation `address[0]` instead of the property name `address.houseNumber`. + // This code should log out the houseNumber from the address object // but it isn't working... // Fix anything that isn't working @@ -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..06be49433 100644 --- a/Sprint-2/debug/author.js +++ b/Sprint-2/debug/author.js @@ -1,5 +1,9 @@ // Predict and explain first... +// For..of loop only supports iterable objects (arrays), not objects +// Should use For..in instead +// Value needs to be accessed from the object using bracket notation + // 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 +15,6 @@ const author = { alive: true, }; -for (const value of author) { - console.log(value); +for (const value in author) { + console.log(author[value]); } diff --git a/Sprint-2/debug/recipe.js b/Sprint-2/debug/recipe.js index 6cbdd22cd..b70698350 100644 --- a/Sprint-2/debug/recipe.js +++ b/Sprint-2/debug/recipe.js @@ -1,5 +1,7 @@ // Predict and explain first... +// Logging the entire recipe object instead of iterating through each element in 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 +14,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..c61232cca 100644 --- a/Sprint-2/implement/contains.js +++ b/Sprint-2/implement/contains.js @@ -1,3 +1,9 @@ -function contains() {} +function contains(obj, property) { + if (obj === null || obj === undefined) { + return false; + } + + return Object.hasOwn(obj, property); +} module.exports = contains; diff --git a/Sprint-2/implement/contains.test.js b/Sprint-2/implement/contains.test.js index 326bdb1f2..f5ed844d9 100644 --- a/Sprint-2/implement/contains.test.js +++ b/Sprint-2/implement/contains.test.js @@ -1,35 +1,25 @@ const contains = require("./contains.js"); -/* -Implement a function called contains that checks an object contains a -particular property - -E.g. contains({a: 1, b: 2}, 'a') // returns true -as the object contains a key of 'a' - -E.g. contains({a: 1, b: 2}, 'c') // returns false -as the object doesn't contains a key of 'c' -*/ - -// Acceptance criteria: - -// 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 - -// Given an empty object -// When passed to contains -// Then it should return false -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 - -// Given an object with properties -// When passed to contains with a non-existent property name -// Then it should return false - -// Given invalid parameters like an array -// When passed to contains -// Then it should return false or throw an error +describe("contains", () => { + test("contains returns true for existing property", () => { + expect(contains({ a: 1, b: 2 }, "a")).toBe(true); + expect(contains({ a: 1, b: 2 }, "c")).toBe(false); + }); + + test("contains on empty object returns false", () => { + expect(contains({}, "a")).toBe(false); + }); + + test("contains returns true for multiple properties", () => { + expect(contains({ name: "John", age: 30, city: "NY" }, "age")).toBe(true); + }); + + test("contains returns false for non-existent property", () => { + expect(contains({ a: 1, b: 2 }, "c")).toBe(false); + }); + + test("contains returns false for non-object types", () => { + expect(contains(null, "a")).toBe(false); + expect(contains(undefined, "a")).toBe(false); + }); +}); diff --git a/Sprint-2/implement/lookup.js b/Sprint-2/implement/lookup.js index a6746e07f..c62cd5935 100644 --- a/Sprint-2/implement/lookup.js +++ b/Sprint-2/implement/lookup.js @@ -1,5 +1,5 @@ -function createLookup() { - // implementation here +function createLookup(arr) { + return Object.fromEntries(arr); } module.exports = createLookup; diff --git a/Sprint-2/implement/lookup.test.js b/Sprint-2/implement/lookup.test.js index 547e06c5a..eb623f2a4 100644 --- a/Sprint-2/implement/lookup.test.js +++ b/Sprint-2/implement/lookup.test.js @@ -1,35 +1,24 @@ 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 - -Acceptance Criteria: - -Given - - An array of arrays representing country code and currency code pairs - e.g. [['US', 'USD'], ['CA', 'CAD']] - -When - - createLookup function is called with the country-currency array as an argument - -Then - - It should return an object where: - - The keys are the country codes - - The values are the corresponding currency codes - -Example -Given: [['US', 'USD'], ['CA', 'CAD']] - -When -createLookup(countryCurrencyPairs) is called - -Then -It should return: - { - 'US': 'USD', - 'CA': 'CAD' - } -*/ +describe("createLookup", () => { + test("creates a country currency code lookup for multiple codes", () => { + const input = [ + ["US", "USD"], + ["CA", "CAD"], + ]; + const result = createLookup(input); + expect(result).toEqual({ US: "USD", CA: "CAD" }); + }); + + test("creates a lookup with a single code pair", () => { + const input = [["GB", "GBP"]]; + const result = createLookup(input); + expect(result).toEqual({ GB: "GBP" }); + }); + + test("creates an empty lookup with an empty array", () => { + const input = []; + const result = createLookup(input); + expect(result).toEqual({}); + }); +}); diff --git a/Sprint-2/implement/querystring.js b/Sprint-2/implement/querystring.js index 45ec4e5f3..32b8c54af 100644 --- a/Sprint-2/implement/querystring.js +++ b/Sprint-2/implement/querystring.js @@ -6,8 +6,10 @@ function parseQueryString(queryString) { const keyValuePairs = queryString.split("&"); for (const pair of keyValuePairs) { - const [key, value] = pair.split("="); - queryParams[key] = value; + const [key, ...valueParts] = pair.split("="); + const decodedKey = decodeURIComponent(key); + const decodedValue = decodeURIComponent(valueParts.join("=")); + queryParams[decodedKey] = decodedValue; } return queryParams; diff --git a/Sprint-2/implement/querystring.test.js b/Sprint-2/implement/querystring.test.js index 3e218b789..82628b2e0 100644 --- a/Sprint-2/implement/querystring.test.js +++ b/Sprint-2/implement/querystring.test.js @@ -3,10 +3,29 @@ // Below is one test case for an edge case the implementation doesn't handle well. // Fix the implementation for this test, and try to think of as many other edge cases as possible - write tests and fix those too. -const parseQueryString = require("./querystring.js") +const parseQueryString = require("./querystring.js"); test("parses querystring values containing =", () => { expect(parseQueryString("equation=x=y+1")).toEqual({ - "equation": "x=y+1", + equation: "x=y+1", }); }); + +test("handles empty string", () => { + expect(parseQueryString("")).toEqual({}); +}); + +test("handles key without value", () => { + expect(parseQueryString("key1=value1&key2=&key3=value3")).toEqual({ + key1: "value1", + key2: "", + key3: "value3", + }); +}); + +test("should decode URL-encoded query strings", () => { + const queryString = "tags%5B%5D=hello%20world"; + const expected = { "tags[]": "hello world" }; + const result = parseQueryString(queryString); + expect(result).toEqual(expected); +}); diff --git a/Sprint-2/implement/tally.js b/Sprint-2/implement/tally.js index f47321812..9578cb2fe 100644 --- a/Sprint-2/implement/tally.js +++ b/Sprint-2/implement/tally.js @@ -1,3 +1,18 @@ -function tally() {} +function tally(arr) { + if (!Array.isArray(arr)) { + throw new Error(); + } + + let result = Object.create(null); + + for (const element of arr) { + if (element in result) { + result[element] += 1; + } else { + result[element] = 1; + } + } + return result; +} module.exports = tally; diff --git a/Sprint-2/implement/tally.test.js b/Sprint-2/implement/tally.test.js index 2ceffa8dd..499068667 100644 --- a/Sprint-2/implement/tally.test.js +++ b/Sprint-2/implement/tally.test.js @@ -1,34 +1,28 @@ const tally = require("./tally.js"); -/** - * tally array - * - * In this task, you'll need to implement a function called tally - * that will take a list of items and count the frequency of each item - * in an array - * - * For example: - * - * tally(['a']), target output: { a: 1 } - * tally(['a', 'a', 'a']), target output: { a: 3 } - * tally(['a', 'a', 'b', 'c']), target output: { a : 2, b: 1, c: 1 } - */ +describe("tally", () => { + test("tally on an empty array returns an empty object", () => { + expect(tally([])).toEqual({}); + }); -// Acceptance criteria: + test("tally with single item returns object with count of 1", () => { + expect(tally(["a"])).toEqual({ a: 1 }); + }); -// 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("tally with multiple items returns counts for each", () => { + expect(tally(["a", "a", "a"])).toEqual({ a: 3 }); + expect(tally(["a", "a", "b", "c"])).toEqual({ a: 2, b: 1, c: 1 }); + expect(tally(["x", "y", "x", "z", "y", "x"])).toEqual({ x: 3, y: 2, z: 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 with invalid input throws an error", () => { + expect(() => tally("invalid")).toThrow(); + expect(() => tally(null)).toThrow(); + expect(() => tally(undefined)).toThrow(); + }); -// Given an array with duplicate items -// When passed to tally -// Then it should return counts for each unique item - -// Given an invalid input like a string -// When passed to tally -// Then it should throw an error + test("tally returns an object with no inherited properties", () => { + const result = tally(["a", "b", "a"]); + expect(Object.getPrototypeOf(result)).toBeNull(); + }); +}); diff --git a/Sprint-2/interpret/invert.js b/Sprint-2/interpret/invert.js index bb353fb1f..6557bd307 100644 --- a/Sprint-2/interpret/invert.js +++ b/Sprint-2/interpret/invert.js @@ -10,20 +10,28 @@ function invert(obj) { const invertedObj = {}; for (const [key, value] of Object.entries(obj)) { - invertedObj.key = value; + invertedObj[value] = key; } return invertedObj; } // 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? +// Returns an array of key/values of the given object +// It is needed so that both the key and value can be destructured from the object and reassigned in the for..of loop // d) Explain why the current return value is different from the target output +// Dot notation is being used in the for..of loop, bracket notation should be used instead as it will become a computed property +// value and key should also be swapped inside the for..of loop // e) Fix the implementation of invert (and write tests to prove it's fixed!) +module.exports = invert; diff --git a/Sprint-2/interpret/invert.test.js b/Sprint-2/interpret/invert.test.js new file mode 100644 index 000000000..262abf372 --- /dev/null +++ b/Sprint-2/interpret/invert.test.js @@ -0,0 +1,27 @@ +const invert = require("./invert.js"); + +describe("invert", () => { + test("inverts an empty object", () => { + const input = {}; + const result = invert(input); + expect(result).toEqual({}); + }); + + test("inverts a single key-value pair", () => { + const input = { a: 1 }; + const result = invert(input); + expect(result).toEqual({ 1: "a" }); + }); + + test("inverts multiple key-value pairs", () => { + const input = { a: 1, b: 2 }; + const result = invert(input); + expect(result).toEqual({ 1: "a", 2: "b" }); + }); + + test("inverts an object with string values", () => { + const input = { x: "hello", y: "world" }; + const result = invert(input); + expect(result).toEqual({ hello: "x", world: "y" }); + }); +});