diff --git a/Sprint-2/debug/address.js b/Sprint-2/debug/address.js index 940a6af83..a3217b713 100644 --- a/Sprint-2/debug/address.js +++ b/Sprint-2/debug/address.js @@ -1,5 +1,6 @@ // Predict and explain first... - +// the const is an object, not an array, so [0] in the console log isn't correc +t // This code should log out the houseNumber from the address object // but it isn't working... // Fix anything that isn't working @@ -9,7 +10,7 @@ const address = { street: "Imaginary Road", city: "Manchester", country: "England", - postcode: "XYZ 123", + 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..3b3d1a776 100644 --- a/Sprint-2/debug/author.js +++ b/Sprint-2/debug/author.js @@ -1,5 +1,5 @@ // Predict and explain first... - +// we should take the object and access to the values through dotation. // 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) { +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..6822a4e1f 100644 --- a/Sprint-2/debug/recipe.js +++ b/Sprint-2/debug/recipe.js @@ -10,6 +10,6 @@ const recipe = { ingredients: ["olive oil", "tomatoes", "salt", "pepper"], }; -console.log(`${recipe.title} serves ${recipe.serves} - ingredients: -${recipe}`); +console.log(`${recipe.title}`); +console.log(`serves: ${recipe.serves}`); +recipe.ingredients.forEach(i => console.log("-"+i)); \ No newline at end of file diff --git a/Sprint-2/implement/contains.js b/Sprint-2/implement/contains.js index cd779308a..9d23d92cf 100644 --- a/Sprint-2/implement/contains.js +++ b/Sprint-2/implement/contains.js @@ -1,3 +1,23 @@ -function contains() {} +function contains(obj, prop) { + + if (typeof obj !== "object" && obj === null ) { //typeof to check if the key is an object + return false; + } else if (Array.isArray(obj)) { //array.isarray to check that the obj is an array, so we throw an error message + throw new Error ("invalid input"); + } + + return prop in obj; //in method to check if a property is in the object + +} +console.log(contains({ a: 1, b: 2 }, "a")); +console.log(contains({ a: 1, b: 2 }, "c")); +console.log(contains({})); +console.log(contains({undefined},"b")); + +try { + console.log(contains([1, 2, 3], "3")); +} catch (E) { + console.log(E.message); +} module.exports = contains; diff --git a/Sprint-2/implement/contains.test.js b/Sprint-2/implement/contains.test.js index 326bdb1f2..a3170e07b 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("contains return true for existing property", () => { + const obj = {a:5, b:10}; + expect(contains(obj, "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 return false for a non-existent property", () => { + const obj = { a: 5, b: 10 }; + expect(contains(obj, "d")).toBe(false); +}); // Given invalid parameters like an array // When passed to contains // Then it should return false or throw an error +test("contains return throw an error message for invalid parameters like an array", () => { + const arr = ["a", "b", "c", "d"]; + expect(() => contains(arr, "d")).toThrow("invalid input"); +}); \ No newline at end of file diff --git a/Sprint-2/implement/lookup.js b/Sprint-2/implement/lookup.js index a6746e07f..6a4466d7b 100644 --- a/Sprint-2/implement/lookup.js +++ b/Sprint-2/implement/lookup.js @@ -1,5 +1,12 @@ -function createLookup() { - // implementation here -} +function createLookup(codePairs) { + if (!Array.isArray(codePairs)) { + return null; + } + + return Object.fromEntries(codePairs); //convert the code pairs arrays into object + +} + + module.exports = createLookup; diff --git a/Sprint-2/implement/lookup.test.js b/Sprint-2/implement/lookup.test.js index 547e06c5a..eae40a747 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,29 @@ It should return: 'CA': 'CAD' } */ + +test("returns empty object when passed an empty array", () => { + expect(createLookup([])).toEqual({}); +}); + +test("returns null when passed a non-array input", () => { + expect(createLookup("hello")).toBeNull(); + expect(createLookup({})).toBeNull(); + expect(createLookup(123)).toBeNull(); +}); + +test("creates a lookup object from an array of code pairs", () => { + const countryCurrency = [ + ["US", "USD"], + ["CA", "CAD"], + ["MX", "MXN"], + ]; + + const expected = { + US: "USD", + CA: "CAD", + MX: "MXN", + }; + + expect(createLookup(countryCurrency)).toEqual(expected); +}); \ No newline at end of file diff --git a/Sprint-2/implement/querystring.js b/Sprint-2/implement/querystring.js index 45ec4e5f3..b1ccb095f 100644 --- a/Sprint-2/implement/querystring.js +++ b/Sprint-2/implement/querystring.js @@ -6,11 +6,12 @@ function parseQueryString(queryString) { const keyValuePairs = queryString.split("&"); for (const pair of keyValuePairs) { - const [key, value] = pair.split("="); + const [key, ...rest] = pair.split("="); //split the first = from the key + const value = rest.join("="); //join the value queryParams[key] = value; - } + } return queryParams; -} +} module.exports = parseQueryString; diff --git a/Sprint-2/implement/querystring.test.js b/Sprint-2/implement/querystring.test.js index 3e218b789..2f120598f 100644 --- a/Sprint-2/implement/querystring.test.js +++ b/Sprint-2/implement/querystring.test.js @@ -10,3 +10,15 @@ test("parses querystring values containing =", () => { "equation": "x=y+1", }); }); + +test("multiple key-value pairs", () => { + expect(parseQueryString("a=1&b=2")).toEqual({ a: "1", b: "2" }); +}); + +test("only = character", () => { + expect(parseQueryString("=")).toEqual({ "": "" }); +}); + +test("empty key with value", () => { + expect(parseQueryString("=value")).toEqual({ "": "value" }); +}); diff --git a/Sprint-2/implement/tally.js b/Sprint-2/implement/tally.js index f47321812..a9e53d26a 100644 --- a/Sprint-2/implement/tally.js +++ b/Sprint-2/implement/tally.js @@ -1,3 +1,22 @@ -function tally() {} +function tally(arr) { + + if (!Array.isArray(arr)) { + throw new TypeError("Input must be an array"); + } + + const counts = {}; //store the result as an object + + for (const items of arr) { //for... of loop to count the items + if (counts[items]) { + counts[items] += 1; + } else { + counts[items] = 1; + } + } + + return counts; + +} + module.exports = tally; diff --git a/Sprint-2/implement/tally.test.js b/Sprint-2/implement/tally.test.js index 2ceffa8dd..019ffdf97 100644 --- a/Sprint-2/implement/tally.test.js +++ b/Sprint-2/implement/tally.test.js @@ -19,16 +19,29 @@ 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("return an object containing the count for each unique item", () => { + const arr = ["a", "a", "b", "c"]; + const expected = { a: 2, b: 1, c: 1 }; + expect(tally(arr)).toEqual(expected); +}); // 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("should return an empty object for an empty array input", () => { + expect(tally([])).toEqual({}); +}); // Given an array with duplicate items // When passed to tally // Then it should return counts for each unique item - +test("should return an object with the counts of each unique item for an input with duplicates", () => { + const dup = ["a", "a", "c"]; + const expected = {a:2, c:1}; + expect(tally(dup)).toEqual(expected); +}); // Given an invalid input like a string // When passed to tally // Then it should throw an error +test("throw an error given a valid input like a string", () => { + expect(() => tally("hello")).toThrow("Input must be an array"); +}); \ No newline at end of file diff --git a/Sprint-2/interpret/invert.js b/Sprint-2/interpret/invert.js index bb353fb1f..dd6afadeb 100644 --- a/Sprint-2/interpret/invert.js +++ b/Sprint-2/interpret/invert.js @@ -10,20 +10,34 @@ 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 } +// 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 } +// 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} +// 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? +// c) What does Object.entries return? Why is it needed in this program? = returns an array of key, value pairs. It's important bc we can loop both, key and values. -// d) Explain why the current return value is different from the target output +// d) Explain why the current return value is different from the target output = because invertedObj.key is just setting a property called "key" +// we need to use the [] to access to the variable. + +// e) Fix the implementation of invert (and write tests to prove it's fixed!) + +test("inverts object with numeric strings value", () => { + expect(invert({ "a": 1, "b": 2 })).toEqual({ 1: "a", 2: "b" }); +}); + +test("inverts object with strings value", () => { + expect(invert({ "a": "hello", "b": "world" })).toEqual({ "hello": "a", "world": "b" }); +}); + +test("inverts object with numeric strings value and literal strings value", () => { + expect(invert({ "a": "hello", "b": 5 })).toEqual({ hello: "a", 5 : "b" }); +}); -// e) Fix the implementation of invert (and write tests to prove it's fixed!) diff --git a/Sprint-3/prep/exerciseprep.html b/Sprint-3/prep/exerciseprep.html new file mode 100644 index 000000000..a3bef2628 --- /dev/null +++ b/Sprint-3/prep/exerciseprep.html @@ -0,0 +1,24 @@ + + +
+ + +You have 200 characters remaining
+