diff --git a/Sprint-2/debug/address.js b/Sprint-2/debug/address.js index 940a6af83..fde180d6d 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 8c2125977..441096d65 100644 --- a/Sprint-2/debug/author.js +++ b/Sprint-2/debug/author.js @@ -11,6 +11,7 @@ const author = { alive: true, }; -for (const value of author) { +for (const value of Object.keys(author)) { console.log(value); -} +} +// An object is not iterable. \ No newline at end of file diff --git a/Sprint-2/debug/recipe.js b/Sprint-2/debug/recipe.js index 6cbdd22cd..9e04261cc 100644 --- a/Sprint-2/debug/recipe.js +++ b/Sprint-2/debug/recipe.js @@ -10,6 +10,7 @@ const recipe = { ingredients: ["olive oil", "tomatoes", "salt", "pepper"], }; + console.log(`${recipe.title} serves ${recipe.serves} - ingredients: -${recipe}`); + ingredients: +${recipe.ingredients.join('\n')}`); //\n diff --git a/Sprint-2/implement/contains.js b/Sprint-2/implement/contains.js index cd779308a..9f67674ee 100644 --- a/Sprint-2/implement/contains.js +++ b/Sprint-2/implement/contains.js @@ -1,3 +1,6 @@ -function contains() {} - +function contains(object,property) { +// contains({a: 1, b: 2}, 'c') // returns false + return object.hasOwnProperty(property); +} +//console.log(contains({a:1, b:2},'a')) module.exports = contains; diff --git a/Sprint-2/implement/contains.test.js b/Sprint-2/implement/contains.test.js index 326bdb1f2..cd00eef4c 100644 --- a/Sprint-2/implement/contains.test.js +++ b/Sprint-2/implement/contains.test.js @@ -20,7 +20,7 @@ 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.todo("contains on empty object returns false"); // Given an object with properties // When passed to contains with an existing property name @@ -33,3 +33,15 @@ test.todo("contains on empty object returns false"); // Given invalid parameters like an array // When passed to contains // Then it should return false or throw an error +describe("contains", () => { + [ + { input: [{a: 1, b: 2}, 'b'], expected: true}, + { input: [{a: 1, b: 2}, 'c'], expected: false}, + { input: [{},'a'], expected: false}, + { input: [[],'a'], expected: false}, + + ].forEach(({input, expected}) => + it(`Expected to check the object contains a particular property, for: [${input}]`,() => expect(contains(...input)).toEqual(expected)) + + ); +}); \ No newline at end of file diff --git a/Sprint-2/implement/lookup.js b/Sprint-2/implement/lookup.js index a6746e07f..06a2ba157 100644 --- a/Sprint-2/implement/lookup.js +++ b/Sprint-2/implement/lookup.js @@ -1,5 +1,12 @@ -function createLookup() { +function createLookup(countryCurrencyPairs) { // implementation here + let objectLookup = {} + for (i = 0 ; i< countryCurrencyPairs.length ; i++){ + //const [country, currency] = countryCurrencyPairs[i] + //objectLookup[country] = currency; + objectLookup[countryCurrencyPairs[i][0]] = countryCurrencyPairs[i][1]; + } + return objectLookup } module.exports = createLookup; diff --git a/Sprint-2/implement/lookup.test.js b/Sprint-2/implement/lookup.test.js index 547e06c5a..ff7cb699a 100644 --- a/Sprint-2/implement/lookup.test.js +++ b/Sprint-2/implement/lookup.test.js @@ -1,6 +1,6 @@ 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"); /* @@ -33,3 +33,13 @@ It should return: 'CA': 'CAD' } */ + +describe("createLookup", () => { + [ + { input: [['US', 'USD'], ['CA', 'CAD']], expected: {'US': 'USD', 'CA': 'CAD'}}, + + ].forEach(({input, expected}) => + it(`It should Create a lookup object of key value pairs from an array of code pairs [${input}]`,() => expect(createLookup(input)).toEqual(expected)) + + ); +}); diff --git a/Sprint-2/implement/querystring.js b/Sprint-2/implement/querystring.js index 45ec4e5f3..f56f6853e 100644 --- a/Sprint-2/implement/querystring.js +++ b/Sprint-2/implement/querystring.js @@ -6,11 +6,11 @@ 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("=", 2); + const [key, value] = [pair.slice(0,pair.indexOf('=')),pair.slice(pair.indexOf('=')+1)]; + queryParams[key] = value; } - return queryParams; } - +console.log(parseQueryString("a=1&b=2")) module.exports = parseQueryString; diff --git a/Sprint-2/implement/querystring.test.js b/Sprint-2/implement/querystring.test.js index 3e218b789..ca706b35d 100644 --- a/Sprint-2/implement/querystring.test.js +++ b/Sprint-2/implement/querystring.test.js @@ -7,6 +7,22 @@ const parseQueryString = require("./querystring.js") test("parses querystring values containing =", () => { expect(parseQueryString("equation=x=y+1")).toEqual({ - "equation": "x=y+1", + equation: "x=y+1", }); }); + +test("parses querystring with multiple key-value pairs", () => { + expect(parseQueryString("a=1&b=2")).toEqual({ + a: "1", + b: "2", + }); +}); + +test("Handle more than 1 pair in the query string", () => { + expect(parseQueryString("sort=newest&color=blue")).toEqual({ + sort: "newest", + color: "blue", + }); +}); + + diff --git a/Sprint-2/implement/tally.js b/Sprint-2/implement/tally.js index f47321812..cdf6f0d91 100644 --- a/Sprint-2/implement/tally.js +++ b/Sprint-2/implement/tally.js @@ -1,3 +1,19 @@ -function tally() {} +function tally(listItems) { + if (typeof listItems === "string" || !Array.isArray(listItems)) return new Error("Input must be array"); + if (listItems.length === 0) return {}; + let objectItems = {}; + + for (const item of listItems) { + //objectItems[item] = (objectItems[item] || 0) + 1; + if (objectItems[item]) { + objectItems[item] += 1; + } else { + objectItems[item] = 1; + } + } + + return objectItems; +} +console.log(tally(['a','a','a','b','c','c','a'])) module.exports = tally; diff --git a/Sprint-2/implement/tally.test.js b/Sprint-2/implement/tally.test.js index 2ceffa8dd..89ba25523 100644 --- a/Sprint-2/implement/tally.test.js +++ b/Sprint-2/implement/tally.test.js @@ -23,7 +23,7 @@ const tally = require("./tally.js"); // 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.todo("tally on an empty array returns an empty object"); // Given an array with duplicate items // When passed to tally @@ -32,3 +32,15 @@ test.todo("tally on an empty array returns an empty object"); // Given an invalid input like a string // When passed to tally // Then it should throw an error +describe('tally',() =>{ + [ + {input:['a'], expected:{ a: 1 }}, + {input:['a', 'a', 'a'], expected:{ a: 3 }}, + {input:['a', 'a', 'b', 'c'], expected:{ a : 2, b: 1, c: 1 }}, + {input:[], expected:{}}, + {input: 'String', expected: new Error("Input must be array")}, + ].forEach(({input,expected})=> + it(`return an object containing the count for each unique item for [${input}]`,()=> + expect(tally(input)).toEqual(expected)) +); +}); \ No newline at end of file diff --git a/Sprint-2/interpret/invert.js b/Sprint-2/interpret/invert.js index bb353fb1f..b25dd2755 100644 --- a/Sprint-2/interpret/invert.js +++ b/Sprint-2/interpret/invert.js @@ -10,20 +10,22 @@ 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 } +} +//19 a) What is the current return value when invert is called with { a : 1 } +// After the function invert() is called with the parameter { a : 1 }, it returns: { key : 1 } // b) What is the current return value when invert is called with { a: 1, b: 2 } - +// After the function invert() is called with the parameter { a: 1, b: 2 }, it returns: { key : 2 } // c) What is the target return value when invert is called with {a : 1, b: 2} - +// After the function invert() with the parameter { a: 1, b: 2 }, the target return value is: { 1 : a , 2 : b } // c) What does Object.entries return? Why is it needed in this program? - +// Object.entries return an Array of key-value pair arrays from obj. It is useful but not strictly necessary. // d) Explain why the current return value is different from the target output - +// Because in the line 13 the invertedObj.key statement is not using the value of the key variable. // e) Fix the implementation of invert (and write tests to prove it's fixed!) +// Done! +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..701a991eb --- /dev/null +++ b/Sprint-2/interpret/invert.test.js @@ -0,0 +1,12 @@ +const invert = require("./invert.js"); +// E.g. invert({x : 10, y : 20}), target output: {"10": "x", "20": "y"} +describe("invert", () =>{ + [ + {input:{x : 10, y : 20}, expected:{"10": "x", "20": "y"}}, + {input:{ a : 1 }, expected:{ 1 : "a" }}, + {input:{a : 1, b: 2}, expected:{ 1 : "a" , 2 : "b" }}, + ].forEach(({input,expected}) => + it(`return inverted key / value object, for [${input}]`,()=> + expect(invert(input)).toEqual(expected)) +) +});