diff --git a/.gitignore b/.gitignore index bde36e530..bc563efa0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ node_modules .DS_Store .vscode -**/.DS_Store \ No newline at end of file +**/.DS_Store +prep \ No newline at end of file diff --git a/Sprint-2/debug/address.js b/Sprint-2/debug/address.js index 940a6af83..eba953a5d 100644 --- a/Sprint-2/debug/address.js +++ b/Sprint-2/debug/address.js @@ -1,5 +1,7 @@ // Predict and explain first... - +/* +My prediction why code isn't working is that we can't access properties in object using index. We need to call the by key value +*/ // 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..4a32e31ff 100644 --- a/Sprint-2/debug/author.js +++ b/Sprint-2/debug/author.js @@ -1,5 +1,7 @@ // Predict and explain first... - +/* +I think the problem is because we try access property in object without saying what exactly property we need +*/ // 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 +13,6 @@ const author = { alive: true, }; -for (const value of author) { - console.log(value); +for (const key in author) { + console.log(author[key]); } diff --git a/Sprint-2/debug/recipe.js b/Sprint-2/debug/recipe.js index 6cbdd22cd..52cbcd365 100644 --- a/Sprint-2/debug/recipe.js +++ b/Sprint-2/debug/recipe.js @@ -1,5 +1,7 @@ // Predict and explain first... - +/* +I think we need to use iteration to access and log ingredients one by one +*/ // 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? @@ -11,5 +13,7 @@ const recipe = { }; console.log(`${recipe.title} serves ${recipe.serves} - ingredients: -${recipe}`); + ingredients:`); +for (let ingredientIndex in recipe.ingredients) { + console.log(recipe.ingredients[ingredientIndex]); +} diff --git a/Sprint-2/implement/contains.js b/Sprint-2/implement/contains.js index cd779308a..caa4e1d21 100644 --- a/Sprint-2/implement/contains.js +++ b/Sprint-2/implement/contains.js @@ -1,3 +1,5 @@ -function contains() {} +function contains(object, property) { + return Object.keys(object).includes(property); +} module.exports = contains; diff --git a/Sprint-2/implement/contains.test.js b/Sprint-2/implement/contains.test.js index 326bdb1f2..5b9542728 100644 --- a/Sprint-2/implement/contains.test.js +++ b/Sprint-2/implement/contains.test.js @@ -16,20 +16,31 @@ as the object doesn't contains a key of 'c' // 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", () => { + // Given an empty object + // When passed to contains + // Then it should return false + test("Given an empty object, return false", () => { + expect(contains({})).toEqual(false); + }); + + // Given an object with properties + // When passed to contains with an existing property name + // Then it should return true + test("Given an object with properties, when passed an existing property name, return true", () => { + expect(contains({ a: 1, b: 2 }, "a")).toEqual(true); + }); + + // Given an object with properties + // When passed to contains with a non-existent property name + // Then it should return false + test("Given an object with properties, when passed a non-existent property name, return false", () => { + expect(contains({ a: 1, b: 2 }, "c")).toEqual(false); + }); + // Given invalid parameters like an array + // When passed to contains + // Then it should return false or throw an error + test("Given invalid parameters like an array, return false or throw an error", () => { + expect(contains(["a", 1, "b", 2, "c"])).toEqual(false); + }); +}); diff --git a/Sprint-2/implement/lookup.js b/Sprint-2/implement/lookup.js index a6746e07f..e6d9e71a0 100644 --- a/Sprint-2/implement/lookup.js +++ b/Sprint-2/implement/lookup.js @@ -1,5 +1,9 @@ -function createLookup() { - // implementation here -} +function createLookup(array) { + const countryCurrencyObject = {}; + for (const index in array) { + countryCurrencyObject[array[index][0]] = array[index][1] + }; + return countryCurrencyObject +}; module.exports = createLookup; diff --git a/Sprint-2/implement/lookup.test.js b/Sprint-2/implement/lookup.test.js index 547e06c5a..c8c0bc247 100644 --- a/Sprint-2/implement/lookup.test.js +++ b/Sprint-2/implement/lookup.test.js @@ -1,6 +1,35 @@ const createLookup = require("./lookup.js"); -test.todo("creates a country currency code lookup for multiple codes"); +describe("function is called with the country-currency array as an argument", () => { + test("It should return an object", () => { + expect( + typeof createLookup([ + ["US", "USD"], + ["CA", "CAD"], + ]) + ).toEqual("object"); + }); + test("The keys are the country codes", () => { + expect( + Object.keys( + createLookup([ + ["US", "USD"], + ["CA", "CAD"], + ]) + ) + ).toEqual(["US", "CA"]); + }); + test("The values are the corresponding currency codes", () => { + expect( + Object.values( + createLookup([ + ["US", "USD"], + ["CA", "CAD"], + ]) + ) + ).toEqual(["USD", "CAD"]); + }); +}); /* diff --git a/Sprint-2/implement/querystring.js b/Sprint-2/implement/querystring.js index 45ec4e5f3..f4eed9b1a 100644 --- a/Sprint-2/implement/querystring.js +++ b/Sprint-2/implement/querystring.js @@ -6,7 +6,9 @@ function parseQueryString(queryString) { const keyValuePairs = queryString.split("&"); for (const pair of keyValuePairs) { - const [key, value] = pair.split("="); + const decodedPair = decodeURIComponent(pair); + const [key, value] = decodedPair.split(/=(.*)/s); + queryParams[key] = value; } diff --git a/Sprint-2/implement/querystring.test.js b/Sprint-2/implement/querystring.test.js index 3e218b789..f99d362ba 100644 --- a/Sprint-2/implement/querystring.test.js +++ b/Sprint-2/implement/querystring.test.js @@ -3,10 +3,16 @@ // 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", }); }); + +test("parses querystring encoded values containing =", () => { + expect(parseQueryString("a%25b=c%26d")).toEqual({ + "a%b": "c&d", + }); +}); diff --git a/Sprint-2/implement/tally.js b/Sprint-2/implement/tally.js index f47321812..775506afa 100644 --- a/Sprint-2/implement/tally.js +++ b/Sprint-2/implement/tally.js @@ -1,3 +1,11 @@ -function tally() {} +function tally(arrayOfItems) { + object = {}; + if (!Array.isArray(arrayOfItems)) { + throw new Error("Invalid input"); + } else { + for (char of arrayOfItems) object[char] = (object[char] || 0) + 1; + return object; + } +} module.exports = tally; diff --git a/Sprint-2/implement/tally.test.js b/Sprint-2/implement/tally.test.js index 2ceffa8dd..509fc168b 100644 --- a/Sprint-2/implement/tally.test.js +++ b/Sprint-2/implement/tally.test.js @@ -19,16 +19,26 @@ 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 +describe("Given a function called tally, 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("tally on an empty array returns an empty object", () => { + expect(tally([])).toEqual({}); + }); -// 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 with duplicate items, return counts for each unique item", () => { + expect(tally(["a", "a", "a"])).toEqual({ a: 3 }); + expect(tally(["a", "a", "b", "c"])).toEqual({ a: 2, b: 1, c: 1 }); + }); -// 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 + // Given an invalid input like a string + // When passed to tally + // Then it should throw an error + test("Given an invalid input like a string, throw an error", () => { + expect(() => tally("a,a,b,c")).toThrow("Invalid input"); + }); +}); diff --git a/Sprint-2/interpret/invert.js b/Sprint-2/interpret/invert.js index bb353fb1f..a0df5479a 100644 --- a/Sprint-2/interpret/invert.js +++ b/Sprint-2/interpret/invert.js @@ -6,24 +6,45 @@ // E.g. invert({x : 10, y : 20}), target output: {"10": "x", "20": "y"} -function invert(obj) { - const invertedObj = {}; +// function invert(obj) { +// const invertedObj = {}; - for (const [key, value] of Object.entries(obj)) { - invertedObj.key = value; - } - - return invertedObj; -} +// for (const [key, value] of Object.entries(obj)) { +// invertedObj.key = value; +// } +// return invertedObj; +// } +// console.log(invert({ a: 1, b: 2 })); // 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? +// It returns an array of string-keyed key-value pairs from the given object // d) Explain why the current return value is different from the target output +// We want to add a property name to the object that is the value of the key variable and not the string "key". +// We can do this with square bracket notation + +// current code creates an object property, where key is key and value is value. +// But we need to swap the key and value. + // e) Fix the implementation of invert (and write tests to prove it's fixed!) +function invert(obj) { + const invertedObj = {}; + + for (const [key, value] of Object.entries(obj)) { + invertedObj[value] = key; + } + + return invertedObj; +} + +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..a1bec199a --- /dev/null +++ b/Sprint-2/interpret/invert.test.js @@ -0,0 +1,8 @@ +const invert = require("./invert.js"); + +describe("proof function is fixed", () => { + test("Given an object, it should swap the keys and values in the object", () => { + expect(invert({ x: 10, y: 20 })).toEqual({ 10: "x", 20: "y" }); + expect(invert({ a: 1, b: 2 })).toEqual({ 1: "a", 2: "b" }); + }); +});