diff --git a/Sprint-2/debug/address.js b/Sprint-2/debug/address.js index 940a6af832..6b8f5d5a12 100644 --- a/Sprint-2/debug/address.js +++ b/Sprint-2/debug/address.js @@ -12,4 +12,7 @@ const address = { postcode: "XYZ 123", }; -console.log(`My house number is ${address[0]}`); +console.log(`My house number is ${address["houseNumber"]}`); +// Predict: reference error will be returned. +// Explain: to access the houseNumber property either dot or bracket notation is needed. +// Solution: address.houseNumber or address["houseNumber"] diff --git a/Sprint-2/debug/author.js b/Sprint-2/debug/author.js index 8c2125977d..ca7bee19ab 100644 --- a/Sprint-2/debug/author.js +++ b/Sprint-2/debug/author.js @@ -11,6 +11,9 @@ const author = { alive: true, }; -for (const value of author) { +for (const value of Object.values(author)) { console.log(value); } + +// Predict: it will return undefined. +// Explain: objects values are accessed by including Object.values(author) diff --git a/Sprint-2/debug/recipe.js b/Sprint-2/debug/recipe.js index 6cbdd22cdc..8db72e4c2e 100644 --- a/Sprint-2/debug/recipe.js +++ b/Sprint-2/debug/recipe.js @@ -11,5 +11,18 @@ const recipe = { }; console.log(`${recipe.title} serves ${recipe.serves} - ingredients: -${recipe}`); +ingredients: +${recipe.ingredients.join("\n")}`); + +// prediction: the above code prints: +// bruschetta +// serves 2 +// ingredients: + +//On a new line it prints: +// title: "bruschetta", +// serves: 2, +// ingredients: "olive oil", "tomatoes", "salt", "pepper" + +// Explain: on line 15 recipe is missing .ingredients. +// As a result it will print the whole property of recipe object. diff --git a/Sprint-2/implement/contains.js b/Sprint-2/implement/contains.js index cd779308a0..0daee9dbe3 100644 --- a/Sprint-2/implement/contains.js +++ b/Sprint-2/implement/contains.js @@ -1,3 +1,13 @@ -function contains() {} +function contains(obj, property) { + if (typeof obj !== "object" || obj === null || Array.isArray(obj)) { + throw new Error("contains requires an object"); + } + for (const key of Object.keys(obj)) { + if (key === property) { + return true; + } + } + return false; +} module.exports = contains; diff --git a/Sprint-2/implement/contains.test.js b/Sprint-2/implement/contains.test.js index 719272787d..10caf59d88 100644 --- a/Sprint-2/implement/contains.test.js +++ b/Sprint-2/implement/contains.test.js @@ -23,18 +23,31 @@ as an array isn't an object // 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("contains with existing property returns true", () => { + 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("contains with a non-existent property returns false", () => { + expect(contains({ a: 1, b: 2 }, "c")).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("contains on non-object returns contains requires an object", () => { + expect(() => contains(([1, 2, 3], "a"))).toThrow( + "contains requires an object" + ); +}); diff --git a/Sprint-2/implement/lookup.js b/Sprint-2/implement/lookup.js index a6746e07f5..5d0ef03ca3 100644 --- a/Sprint-2/implement/lookup.js +++ b/Sprint-2/implement/lookup.js @@ -1,5 +1,10 @@ -function createLookup() { +function createLookup(countryCurrencyPairs) { // implementation here + const currencyLookup = {}; + for (const [country, currency] of countryCurrencyPairs) { + currencyLookup[country] = currency; + } + return currencyLookup; } module.exports = createLookup; diff --git a/Sprint-2/implement/lookup.test.js b/Sprint-2/implement/lookup.test.js index 547e06c5a7..e5bc5f4de6 100644 --- a/Sprint-2/implement/lookup.test.js +++ b/Sprint-2/implement/lookup.test.js @@ -1,6 +1,16 @@ const createLookup = require("./lookup.js"); -test.todo("creates a country currency code lookup for multiple codes"); +test("creates a country currency code lookup for multiple codes", () => { + expect( + createLookup([ + ["US", "USD"], + ["CA", "CAD"], + ]) + ).toEqual({ + US: "USD", + CA: "CAD", + }); +}); /* diff --git a/Sprint-2/implement/querystring.js b/Sprint-2/implement/querystring.js index 45ec4e5f37..c9259c73b8 100644 --- a/Sprint-2/implement/querystring.js +++ b/Sprint-2/implement/querystring.js @@ -4,12 +4,19 @@ function parseQueryString(queryString) { return queryParams; } const keyValuePairs = queryString.split("&"); - for (const pair of keyValuePairs) { - const [key, value] = pair.split("="); - queryParams[key] = value; + if (!pair) { + continue; + } + const everything = pair.split("="); + const key = decodeURIComponent(everything.shift().replace(/\+/g, " ")); + const value = decodeURIComponent(everything.join("=").replace(/\+/g, " ")); + if (queryParams[key]) { + queryParams[key] = [].concat(queryParams[key], value); + } else { + queryParams[key] = value; + } } - return queryParams; } diff --git a/Sprint-2/implement/tally.js b/Sprint-2/implement/tally.js index f47321812e..dcf9716b7b 100644 --- a/Sprint-2/implement/tally.js +++ b/Sprint-2/implement/tally.js @@ -1,3 +1,18 @@ -function tally() {} +function tally(list) { + if (!Array.isArray(list)) { + throw new Error("tally requires an array"); + } + + const itemFrequencyCount = {}; + + for (const item of list) { + if (Object.hasOwn(itemFrequencyCount, item)) { + itemFrequencyCount[item] = itemFrequencyCount[item] + 1; + } else { + itemFrequencyCount[item] = 1; + } + } + return itemFrequencyCount; +} module.exports = tally; diff --git a/Sprint-2/implement/tally.test.js b/Sprint-2/implement/tally.test.js index ca763c2965..8a228351c0 100644 --- a/Sprint-2/implement/tally.test.js +++ b/Sprint-2/implement/tally.test.js @@ -19,16 +19,27 @@ 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("tally on an array of items returns the count", () => { + 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 with duplicate items return duplicate counts", () => { + expect(tally(["a", "a", "a"])).toEqual({ a: 3 }); +}); // 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("tally with invalid input returns tally requires an array", () => { + expect(() => tally("list", 2, " ")).toThrow("tally requires an array"); +}); diff --git a/Sprint-2/interpret/invert.js b/Sprint-2/interpret/invert.js index bb353fb1fd..36797f5f99 100644 --- a/Sprint-2/interpret/invert.js +++ b/Sprint-2/interpret/invert.js @@ -7,23 +7,34 @@ // E.g. invert({x : 10, y : 20}), target output: {"10": "x", "20": "y"} function invert(obj) { + if (typeof obj !== "object") { + throw new Error("invert requires a valid input"); + } 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 } +// - undefined. // b) What is the current return value when invert is called with { a: 1, b: 2 } +// - undefined. // 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 turns object properties into an array of key-value pairs // d) Explain why the current return value is different from the target output +// - line 13 is accessing the property 'key' name directly +// - the target output is to swap properties inside the variable 'key' and 'value' // 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..6d09ffc06a --- /dev/null +++ b/Sprint-2/interpret/invert.test.js @@ -0,0 +1,41 @@ +const invert = require("./invert.js"); + +// Empty object +test("invert on an empty object returns an empty object", () => { + expect(invert({})).toEqual({}); +}); + +// Basic objects +test("invert on basic object returns inverted properties object ", () => { + expect(invert({ x: 10, y: 20 })).toEqual({ 10: "x", 20: "y" }); +}); + +// Single property +test("invert on a single property object returns inverted key-value object", () => { + expect(invert({ a: 1 })).toEqual({ 1: "a" }); +}); + +// Multiple properties +test("invert on multiple properties object returns inverted properties object", () => { + expect(invert({ 1: "a", 2: "b", 3: "c", 4: "d" })).toEqual({ + a: "1", + b: "2", + c: "3", + d: "4", + }); +}); + +// String values +test("invert on string value object returns inverted key-value object", () => { + expect(invert({ a: "hello" })).toEqual({ hello: "a" }); +}); + +// Duplicate values +test("invert on duplicate values object return inverted object", () => { + expect(invert({ a: 1, b: 1 })).toEqual({ 1: "a", 1: "b" }); +}); + +// Invalid input +test("invert on invalid input returns invert requires a valid input", () => { + expect(() => invert()).toThrow("invert requires a valid input"); +});