diff --git a/Sprint-2/debug/address.js b/Sprint-2/debug/address.js index 940a6af83..b2475e804 100644 --- a/Sprint-2/debug/address.js +++ b/Sprint-2/debug/address.js @@ -1,4 +1,6 @@ // Predict and explain first... +// We create an object address which has 5 properties.In order to access the value within a property +// like house number we either need to use the dot notation such as address.houseNumber or square brackets such as address["houseNumber"] // This code should log out the houseNumber from the address object // but it 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..e23b111f8 100644 --- a/Sprint-2/debug/author.js +++ b/Sprint-2/debug/author.js @@ -1,5 +1,6 @@ // Predict and explain first... - +// it doesnt work as objects are not iteraable and there for we cant use for loops. +// But by using object.values allows us to get the value within an object and store it in an array and then we can use for of loop. // 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 +12,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..f0d905f32 100644 --- a/Sprint-2/debug/recipe.js +++ b/Sprint-2/debug/recipe.js @@ -1,5 +1,6 @@ // Predict and explain first... - +// there is a bug in this code, to access values of ingredients we need to use dot notation (recipe.ingredients) to access it values. +// In order to print out ingredients in seperate line we can then use for of loop. // 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 +12,7 @@ const recipe = { }; console.log(`${recipe.title} serves ${recipe.serves} - ingredients: -${recipe}`); +ingredients:`); +for (value of recipe.ingredients) { + console.log(value); +} diff --git a/Sprint-2/implement/contains.js b/Sprint-2/implement/contains.js index cd779308a..7f82e55a8 100644 --- a/Sprint-2/implement/contains.js +++ b/Sprint-2/implement/contains.js @@ -1,3 +1,6 @@ -function contains() {} +function contains(obj, key) { + return Object.hasOwn(obj, key); +} +console.log(contains([1, 2, 3, 4], "ingredients")); module.exports = contains; diff --git a/Sprint-2/implement/contains.test.js b/Sprint-2/implement/contains.test.js index 326bdb1f2..8d46f4e8c 100644 --- a/Sprint-2/implement/contains.test.js +++ b/Sprint-2/implement/contains.test.js @@ -16,20 +16,51 @@ 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 a contains function when passed an object and property name then it should false if the object doesnt contain the property", () => { + currentOutput = contains( + { firstName: "Mansoor", lastName: "Munawar" }, + "hello" + ); + targetOutput = false; + expect(currentOutput).toBe(targetOutput); +}); +// When passed an object and a property name") // 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", () => { + const currentOutput = contains({}); + const targetOutput = false; + expect(currentOutput).toBe(targetOutput); +}); // 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 to contains with existing propery name, it returns true", () => { + const currentOutput = contains( + { firstName: "Mansoor", organisation: "CYF" }, + "firstName" + ); + const targetOutput = true; + expect(currentOutput).toBe(targetOutput); +}); // 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 and when it passed to contains with non-existent property name then it should return false", () => { + const currentOutput = contains( + { firstName: "Mansoor", organisation: "CYF" }, + "Name" + ); + const targetOutput = false; + expect(currentOutput).toBe(targetOutput); +}); // 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 when passed to contains, it returns false or throw an error", () => { + const currentOutput = contains([1, 2, 3, 4], "hello"); + targetOutput = false; + expect(currentOutput).toBe(targetOutput); +}); diff --git a/Sprint-2/implement/lookup.js b/Sprint-2/implement/lookup.js index a6746e07f..2c1d55157 100644 --- a/Sprint-2/implement/lookup.js +++ b/Sprint-2/implement/lookup.js @@ -1,5 +1,15 @@ -function createLookup() { +function createLookup(twoDeminsionalArray) { // implementation here + let result = {}; + for (let [country, currency] of twoDeminsionalArray) { + result[country] = currency; + } + return result; } - +console.log( + createLookup([ + ["US", "USD"], + ["CA", "CAD"], + ]) +); module.exports = createLookup; diff --git a/Sprint-2/implement/lookup.test.js b/Sprint-2/implement/lookup.test.js index 547e06c5a..56a27b49b 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", () => { + let currentOutput = createLookup([ + ["US", "USD"], + ["CA", "CAD"], + ]); + let targetOutput = { + US: "USD", + CA: "CAD", + }; + expect(currentOutput).toEqual(targetOutput); +}); /* diff --git a/Sprint-2/implement/querystring.js b/Sprint-2/implement/querystring.js index 45ec4e5f3..659bc93dc 100644 --- a/Sprint-2/implement/querystring.js +++ b/Sprint-2/implement/querystring.js @@ -6,11 +6,17 @@ function parseQueryString(queryString) { const keyValuePairs = queryString.split("&"); for (const pair of keyValuePairs) { - const [key, value] = pair.split("="); - queryParams[key] = value; + if (!pair.includes("=")) { + continue; + } + const [key, ...value] = pair.split("="); + if (key === "") { + continue; + } + queryParams[key] = value.join("="); } return queryParams; } - +console.log(parseQueryString("=sort&colour=yellow")); module.exports = parseQueryString; diff --git a/Sprint-2/implement/querystring.test.js b/Sprint-2/implement/querystring.test.js index 3e218b789..0f76c87b5 100644 --- a/Sprint-2/implement/querystring.test.js +++ b/Sprint-2/implement/querystring.test.js @@ -3,10 +3,22 @@ // 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", + equation: "x=y+1", }); }); + +test("parses querystring with missing value", () => { + let currentOutput = parseQueryString("sort&colour=yellow"); + let targetOutput = { colour: "yellow" }; + expect(currentOutput).toEqual(targetOutput); +}); + +test("parses querystring with missing key", () => { + let currentOutput = parseQueryString("=dark&colour=yellow"); + let targetOutput = { colour: "yellow" }; + expect(currentOutput).toEqual(targetOutput); +}); diff --git a/Sprint-2/implement/tally.js b/Sprint-2/implement/tally.js index f47321812..85db57b1e 100644 --- a/Sprint-2/implement/tally.js +++ b/Sprint-2/implement/tally.js @@ -1,3 +1,17 @@ -function tally() {} +function tally(arrayOfItems) { + if (!Array.isArray(arrayOfItems)) { + throw new TypeError("parameter must be an array"); + } + let result = {}; + arrayOfItems.forEach((element) => { + if (element in result) { + result[element] += 1; + } else { + result[element] = 1; + } + }); + return result; +} +console.log(tally([])); module.exports = tally; diff --git a/Sprint-2/implement/tally.test.js b/Sprint-2/implement/tally.test.js index 2ceffa8dd..fbf4b7224 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("when function tally called with an array of items, it returns object containing the count for each unique item", () => { + let currentOutput = tally(["a", "a", "a"]); + let targetOutput = { a: 3 }; + expect(currentOutput).toEqual(targetOutput); +}); // 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", () => { + let currentOutput = tally([]); + let targetOutput = {}; + expect(currentOutput).toEqual(targetOutput); +}); // Given an array with duplicate items // When passed to tally // Then it should return counts for each unique item - +test("tally on array of duplicate items, it returns counts for each unique item", () => { + let currentOutput = tally(["a", "a", "b", "c"]); + let targetOutput = { a: 2, b: 1, c: 1 }; + expect(currentOutput).toEqual(targetOutput); +}); // 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 passed to tally, it returns an error", () => { + expect(() => tally("hello")).toThrow(TypeError); +}); diff --git a/Sprint-2/interpret/invert.js b/Sprint-2/interpret/invert.js index bb353fb1f..936dab73d 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; } +console.log(invert({ x: 10, y: 20 })); +module.exports = invert; // a) What is the current return value when invert is called with { a : 1 } - +// it will be {"key" : 1} // b) What is the current return value when invert is called with { a: 1, b: 2 } - +// it will be ["key": 2] // c) What is the target return value when invert is called with {a : 1, b: 2} - +// the target return value needs to be {"1": "a", "2": "b"} // c) What does Object.entries return? Why is it needed in this program? - +// object.entries return an array of each [key] and [value] pair and in this case it is necessary as we need both the key and value from the object. // d) Explain why the current return value is different from the target output - +// there is an bug as we use .key instead of [value]. So instead of taking the variable [value] we are instead creating a property called key which isnt correct. // 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..24bd4e796 --- /dev/null +++ b/Sprint-2/interpret/invert.test.js @@ -0,0 +1,13 @@ +const invert = require("./invert.js"); + +test("When invert is passed an object it should swap the keys and values in the object and then return it", () => { + let currentOutput = invert({ x: 10, y: 20 }); + let targetOutput = { 10: "x", 20: "y" }; + expect(currentOutput).toEqual(targetOutput); +}); + +test("When invert is passed an object it should swap the keys and values in the object and then return it", () => { + let currentOutput = invert({ a: 1, b: 2 }); + let targetOutput = { 1: "a", 2: "b" }; + expect(currentOutput).toEqual(targetOutput); +});