diff --git a/Sprint-2/debug/address.js b/Sprint-2/debug/address.js index 940a6af832..36d2f865db 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 8c2125977d..7ba8f4c754 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 6cbdd22cdc..533ac4ec56 100644 --- a/Sprint-2/debug/recipe.js +++ b/Sprint-2/debug/recipe.js @@ -11,5 +11,7 @@ const recipe = { }; console.log(`${recipe.title} serves ${recipe.serves} - ingredients: -${recipe}`); + ingredients:`); +for (const ingredient of recipe.ingredients) { + console.log(ingredient); +} diff --git a/Sprint-2/implement/contains.js b/Sprint-2/implement/contains.js index cd779308a0..434ad4170d 100644 --- a/Sprint-2/implement/contains.js +++ b/Sprint-2/implement/contains.js @@ -1,3 +1,23 @@ -function contains() {} +function contains(obj, propertyName) { + if (Array.isArray(obj)) { + throw new Error("contains requires an object"); + } + if (typeof obj === "string") { + throw new Error("contains requires an object"); + } + if (typeof obj === "number") { + throw new Error("contains requires an object"); + } + if (obj === null) { + throw new Error("contains requires an object"); + } + if (obj === undefined) { + throw new Error("contains requires an object"); + } + if (Object.keys(obj).length === 0) { + return false; + } + return Object.keys(obj).includes(propertyName); +} module.exports = contains; diff --git a/Sprint-2/implement/contains.test.js b/Sprint-2/implement/contains.test.js index 719272787d..a8cfb2c601 100644 --- a/Sprint-2/implement/contains.test.js +++ b/Sprint-2/implement/contains.test.js @@ -19,22 +19,40 @@ as an array isn't an object // 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("returns true when given an object and property name that contains property", () => { + expect(contains({ a: 1, b: 2 }, "a")).toBe(true); +}); +test("returns false when given an object and property name that doesn't contain the property", () => { + expect(contains({ a: 1, b: 2 }, "c")).toBe(false); +}); // 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 +test("returns false when given an empty object", () => { + expect(contains({})).toBe(false); +}); // Given a value that isn't an object - an array, a string, a number, // null, or no argument at all // When passed to contains // Then it should throw Error("contains requires an object") // (careful: typeof [] and typeof null are both "object") +test("throws an error when given an array", () => { + expect(() => contains([1, 2, 3], "a")).toThrow("contains requires an object"); +}); + +test("throws an error when given a string", () => { + expect(() => contains("hello", "a")).toThrow("contains requires an object"); +}); +test("throws an error when given a number", () => { + expect(() => contains(42, "a")).toThrow("contains requires an object"); +}); +test("throws an error when given null", () => { + expect(() => contains(null, "a")).toThrow("contains requires an object"); +}); + +test("throws an error when given no argument", () => { + expect(() => contains()).toThrow("contains requires an object"); +}); diff --git a/Sprint-2/implement/lookup.js b/Sprint-2/implement/lookup.js index a6746e07f5..1922fa222c 100644 --- a/Sprint-2/implement/lookup.js +++ b/Sprint-2/implement/lookup.js @@ -1,5 +1,9 @@ -function createLookup() { - // implementation here +function createLookup(countryCurrencyPairs) { + const lookup = {}; + for (const pair of countryCurrencyPairs) { + lookup[pair[0]] = pair[1]; + } + return lookup; } module.exports = createLookup; diff --git a/Sprint-2/implement/lookup.test.js b/Sprint-2/implement/lookup.test.js index 547e06c5a7..8eff977107 100644 --- a/Sprint-2/implement/lookup.test.js +++ b/Sprint-2/implement/lookup.test.js @@ -1,7 +1,26 @@ const createLookup = require("./lookup.js"); -test.todo("creates a country currency code lookup for multiple codes"); - +test("given currency code pairs returns an object with the country and its currency code", () => { + expect( + createLookup([ + ["JP", "JPY"], + ["GB", "GBP"], + ]) + ).toEqual({ JP: "JPY", GB: "GBP" }); +}); + +test("given an empty array returns an empty object", () => { + expect(createLookup([])).toEqual({}); +}); + +test("reassigns the previous currency when a country code is duplicated", () => { + expect( + createLookup([ + ["GB", "GBP"], + ["GB", "XXX"], + ]) + ).toEqual({ GB: "XXX" }); +}); /* Create a lookup object of key value pairs from an array of code pairs diff --git a/Sprint-2/implement/querystring.js b/Sprint-2/implement/querystring.js index 45ec4e5f37..01a49839de 100644 --- a/Sprint-2/implement/querystring.js +++ b/Sprint-2/implement/querystring.js @@ -3,10 +3,14 @@ function parseQueryString(queryString) { if (queryString.length === 0) { return queryParams; } - const keyValuePairs = queryString.split("&"); + const keyValuePairs = queryString.split("&").filter((pair) => pair !== ""); for (const pair of keyValuePairs) { - const [key, value] = pair.split("="); + const parts = pair.split("="); + const key = decodeURIComponent(parts[0].replace(/\+/g, " ")); + const value = decodeURIComponent( + parts.slice(1).join("=").replace(/\+/g, " ") + ); queryParams[key] = value; } diff --git a/Sprint-2/implement/querystring.test.js b/Sprint-2/implement/querystring.test.js index 328b8df617..a2f400262c 100644 --- a/Sprint-2/implement/querystring.test.js +++ b/Sprint-2/implement/querystring.test.js @@ -3,7 +3,7 @@ // Below are some test cases the implementation doesn't handle well. // Fix the implementation for these tests, 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("should parse values containing '='", () => { expect(parseQueryString("equation=a=b-2")).toEqual({ @@ -36,13 +36,3 @@ test("should replace '+' by ' '", () => { "full name": "John Doe", }); }); - -// Stretch exercise: Handling query strings that contain identical keys - -// 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", - }); -}); diff --git a/Sprint-2/implement/tally.js b/Sprint-2/implement/tally.js index f47321812e..1065e5bbf1 100644 --- a/Sprint-2/implement/tally.js +++ b/Sprint-2/implement/tally.js @@ -1,3 +1,20 @@ -function tally() {} +function tally(items) { + const counts = {}; + if (!Array.isArray(items)) { + throw new Error("tally requires an array"); + } + + for (let i = 0; i < items.length; i++) { + const item = items[i]; + + if (Object.keys(counts).includes(item)) { + 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 ca763c2965..002fd253d5 100644 --- a/Sprint-2/implement/tally.test.js +++ b/Sprint-2/implement/tally.test.js @@ -19,16 +19,30 @@ 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 - -// 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"); - // Given an array with duplicate items // When passed to tally // Then it should return counts for each unique item +test("given an array of items, returns an object containing the count for each 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("tally on an empty array returns an empty object", () => { + expect(tally([])).toEqual({}); +}); // Given an invalid input like a string, a number, or no argument at all // When passed to tally // Then it should throw Error("tally requires an array") +test("given invalid input like a string, throw an error message", () => { + expect(() => tally("hello")).toThrow("tally requires an array"); +}); + +test("given invalid input like a number, throw an error message", () => { + expect(() => tally(7)).toThrow("tally requires an array"); +}); + +test("given invalid input like no argument, throw an error message", () => { + expect(() => tally()).toThrow("tally requires an array"); +}); diff --git a/Sprint-2/interpret/invert.js b/Sprint-2/interpret/invert.js index bb353fb1fd..107797a77d 100644 --- a/Sprint-2/interpret/invert.js +++ b/Sprint-2/interpret/invert.js @@ -10,20 +10,23 @@ function invert(obj) { const invertedObj = {}; for (const [key, value] of Object.entries(obj)) { - invertedObj.key = value; + invertedObj[value] = key; } return invertedObj; } +module.exports = invert; + // a) What is the current return value when invert is called with { a : 1 } +//When invert({ a: 1 }) is called, the loop assigns the value 1 to a property literally named key. Therefore, the function returns { 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 the key value pairs in separate arrays. It's needed in this program so we can access the keys and values separately. // d) Explain why the current return value is different from the target output - +// It uses dot notation (invertedObj.key), which creates a property called key. It doesn't use the original value as the new property name, so the keys and values aren't swapped correctly. // e) Fix the implementation of invert (and write tests to prove it's fixed!) diff --git a/Sprint-2/interpret/invert.test.js b/Sprint-2/interpret/invert.test.js new file mode 100644 index 0000000000..705d51ce7d --- /dev/null +++ b/Sprint-2/interpret/invert.test.js @@ -0,0 +1,13 @@ +const invert = require("./invert.js"); + +test("given a key-value pair, should return it inverted", () => { + expect(invert({ a: 1 })).toEqual({ 1: "a" }); +}); + +test("given multiple key-value pairs, should return them inverted", () => { + expect(invert({ a: 1, b: 2 })).toEqual({ 1: "a", 2: "b" }); +}); + +test("given an empty object, should return an empty object", () => { + expect(invert({})).toEqual({}); +});