diff --git a/Sprint-2/debug/address.js b/Sprint-2/debug/address.js index 940a6af83..bac134513 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[0]}`); +// Predict result: My house number is undefined. As address[0] is undefined. + +console.log(`My house number is ${address["houseNumber"]}`); diff --git a/Sprint-2/debug/author.js b/Sprint-2/debug/author.js index 8c2125977..dc0c2f3c3 100644 --- a/Sprint-2/debug/author.js +++ b/Sprint-2/debug/author.js @@ -11,6 +11,12 @@ const author = { alive: true, }; -for (const value of author) { +// for (const value of author) { +// console.log(value); +// } + +// Predict: Will get error because for..of loop works only on Iterables + +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..f57550999 100644 --- a/Sprint-2/debug/recipe.js +++ b/Sprint-2/debug/recipe.js @@ -10,6 +10,17 @@ const recipe = { ingredients: ["olive oil", "tomatoes", "salt", "pepper"], }; -console.log(`${recipe.title} serves ${recipe.serves} - ingredients: -${recipe}`); +// console.log(`${recipe.title} serves ${recipe.serves} +// ingredients: +// ${recipe}`); + +// console.log(`${recipe}`); +//Predict result: bruschetta serves 2 ingredients: [object Object] + +console.log(`${recipe.title} +serves ${recipe.serves} +ingredients:`); + +for (let ingredient of recipe.ingredients) { + console.log(ingredient); +} diff --git a/Sprint-2/implement/contains.js b/Sprint-2/implement/contains.js index cd779308a..b75ce8b45 100644 --- a/Sprint-2/implement/contains.js +++ b/Sprint-2/implement/contains.js @@ -1,3 +1,16 @@ -function contains() {} +function contains(object, propertyName) { + // console.log(propertyName, '<-- property name'); + // if (!propertyName) return false; + // if (!object) return false; + if (typeof object !== "object" || object === null || Array.isArray(object) || typeof object === "string") return false; + if (object.hasOwnProperty(propertyName)) return true; + else return false; +}; module.exports = contains; + +console.log(contains({a: 1, b: 2}, 'a')); +console.log(contains({a: 1, b: 2})); +console.log(contains()); +console.log(contains({a: 1, b: 2}, 'c')); +console.log(contains([1,2,3])); diff --git a/Sprint-2/implement/contains.test.js b/Sprint-2/implement/contains.test.js index 326bdb1f2..9d1299a24 100644 --- a/Sprint-2/implement/contains.test.js +++ b/Sprint-2/implement/contains.test.js @@ -16,20 +16,40 @@ 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 +test("given an object and property arg returns true", () => { + expect(contains({a: 1, b: 2}, 'a')).toEqual(true); +}); + +test("given an object, but no property arg then returns false", () => { +expect(contains({a: 1, b: 2})).toEqual(false); +}); // 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")).toEqual(false); +}); // Given an object with properties // When passed to contains with an existing property name // Then it should return true +test("contains the property in the object returns 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("not contains the property in the object returns 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("contains invalid parameters return false", () => { + expect(contains([1,2,3], '0')).toEqual(false); + expect(contains("ABC", '0')).toEqual(false); + expect(contains(null, 'a')).toEqual(false); +}); \ No newline at end of file diff --git a/Sprint-2/implement/lookup.js b/Sprint-2/implement/lookup.js index a6746e07f..3f5be58e1 100644 --- a/Sprint-2/implement/lookup.js +++ b/Sprint-2/implement/lookup.js @@ -1,5 +1,8 @@ -function createLookup() { +function createLookup(countryCurrencyPairs) { // implementation here -} + if (countryCurrencyPairs !== undefined) return Object.fromEntries(countryCurrencyPairs); +}; module.exports = createLookup; + +console.log(createLookup([['US', 'USD'], ['CA', 'CAD'], ["HK", 'HKD']])); \ No newline at end of file diff --git a/Sprint-2/implement/lookup.test.js b/Sprint-2/implement/lookup.test.js index 547e06c5a..fa717c66f 100644 --- a/Sprint-2/implement/lookup.test.js +++ b/Sprint-2/implement/lookup.test.js @@ -1,6 +1,11 @@ const createLookup = require("./lookup.js"); -test.todo("creates a country currency code lookup for multiple codes"); +// 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 45ec4e5f3..475e3758d 100644 --- a/Sprint-2/implement/querystring.js +++ b/Sprint-2/implement/querystring.js @@ -6,11 +6,15 @@ function parseQueryString(queryString) { const keyValuePairs = queryString.split("&"); for (const pair of keyValuePairs) { - const [key, value] = pair.split("="); - queryParams[key] = value; + const [key, value] = pair.split(/=(.*)/s); + // queryParams[key] = value; + queryParams[decodeURIComponent(key)] = decodeURIComponent(value); } return queryParams; } module.exports = parseQueryString; + +console.log(parseQueryString("equation=x=y+1")); +console.log(parseQueryString("5%25")); \ No newline at end of file diff --git a/Sprint-2/implement/querystring.test.js b/Sprint-2/implement/querystring.test.js index 3e218b789..eb9b6a5f7 100644 --- a/Sprint-2/implement/querystring.test.js +++ b/Sprint-2/implement/querystring.test.js @@ -10,3 +10,4 @@ test("parses querystring values containing =", () => { "equation": "x=y+1", }); }); + diff --git a/Sprint-2/implement/tally.js b/Sprint-2/implement/tally.js index f47321812..f46aa4182 100644 --- a/Sprint-2/implement/tally.js +++ b/Sprint-2/implement/tally.js @@ -1,3 +1,16 @@ -function tally() {} +function tally(items) { + let countLetter = {}; + if (Array.isArray(items) === false) throw new Error("You must provide an item"); + else if (items.length === 0) return countLetter; + else { + items.forEach(letter => countLetter[letter] ? countLetter[letter]++ : countLetter[letter] = 1) + return countLetter; + }; +}; module.exports = tally; + +console.log(tally(['a', 'a', 'a'])); // output: { a: 3 } +console.log(tally(['a', 'b', 'c'])); // output: { a: 1, b: 1, c: 1 } +console.log(tally([])); // output: {} +// console.log(tally('Hello')); // output: Throw Error "You must provide an item" \ No newline at end of file diff --git a/Sprint-2/implement/tally.test.js b/Sprint-2/implement/tally.test.js index 2ceffa8dd..239c83c32 100644 --- a/Sprint-2/implement/tally.test.js +++ b/Sprint-2/implement/tally.test.js @@ -19,16 +19,31 @@ 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 each unique item on an array returns the count", () => { + expect(tally(['a', 'b', 'c'])).toEqual({ a: 1, 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 on each unique item on an array with duplicate items returns the count for each unique item", () => { + expect(tally(['a', 'a', 'a'])).toEqual({ a: 3 }); +}); // Given an invalid input like a string // When passed to tally // Then it should throw an error +test("Given an invalid input throw an error", () => { + expect( () => { tally('Hello') } ).toThrow("You must provide an item"); +}); + diff --git a/Sprint-2/interpret/invert.js b/Sprint-2/interpret/invert.js index bb353fb1f..13387c42d 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; } +module.exports = invert; + +console.log(invert({x : 10, y : 20})); //output: {"10": "x", "20": "y"} +console.log(invert({ a: 1, b: 2 })); // output: {"1": "a", "2": "b"} + // 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? +// Object.entries returns an array of the key/pairs of an object, it makes it simple to use objects in loops. +// In this case, if Object.entries is removed, it would occur error showing obj is not iterable. +// Therefore, Object.entries is needed as it changed obj to array so that it can be looped over. // d) Explain why the current return value is different from the target output +// There are two problems in the current function +// 1. key and value haven't been swap +// 2. invertedObj.key is unable to return the key of the object // 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 000000000..c77e93c7c --- /dev/null +++ b/Sprint-2/interpret/invert.test.js @@ -0,0 +1,5 @@ +const invert = require("./invert.js"); + +test("Given an object, then swap the keys and values in the object", () => { + expect(invert({x : 10, y : 20})).toEqual({"10": "x", "20": "y"}); +});