diff --git a/Sprint-2/debug/address.js b/Sprint-2/debug/address.js index 940a6af83..3514c69e5 100644 --- a/Sprint-2/debug/address.js +++ b/Sprint-2/debug/address.js @@ -1,5 +1,5 @@ // Predict and explain first... - +// it returns undefined as it can't find 0 because this is an object its iterating with and not an array // This code should log out the houseNumber from the address object // but it isn't working... // Fix anything that isn't working @@ -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..59cef7b77 100644 --- a/Sprint-2/debug/author.js +++ b/Sprint-2/debug/author.js @@ -1,5 +1,6 @@ // Predict and explain first... - +// value is undefined, its not seen in the code at all. +// Ah error said it is not iterable, i didnt spot the for...of loop there, so its not seeing the value as an option in the object. // 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..fc82e33e4 100644 --- a/Sprint-2/debug/recipe.js +++ b/Sprint-2/debug/recipe.js @@ -1,5 +1,6 @@ // Predict and explain first... - +// it does not make a new line as just pressing enter wont work for that. +// I was wrong, ingredients is showing [object Object], as it wont log out a full object like this. // 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? @@ -12,4 +13,4 @@ const recipe = { console.log(`${recipe.title} serves ${recipe.serves} ingredients: -${recipe}`); +${recipe.ingredients.join("\n")}`); diff --git a/Sprint-2/implement/contains.js b/Sprint-2/implement/contains.js index cd779308a..1efb69e87 100644 --- a/Sprint-2/implement/contains.js +++ b/Sprint-2/implement/contains.js @@ -1,3 +1,8 @@ -function contains() {} +function contains(object, property) { + if (Array.isArray(object)) { + throw new Error("contains requires an object"); + } + return property in object; +} module.exports = contains; diff --git a/Sprint-2/implement/contains.test.js b/Sprint-2/implement/contains.test.js index 719272787..2dfa7b9fa 100644 --- a/Sprint-2/implement/contains.test.js +++ b/Sprint-2/implement/contains.test.js @@ -19,22 +19,36 @@ as an array isn't an object // 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("When passed an object and a property name, returns true", () => { + expect(contains({ a: 1, b: 2 }, "a")).toBe(true); +}); // Given an empty object // When passed to contains // Then it should return false -test.todo("contains on empty object returns false"); +test("Given an 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("Given an object with properties, returns true", () => { + expect(contains({ a: 1, b: 2 }, "a")).toBe(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 a non-existent property name, returns false", () => { + expect(contains({ a: 1, b: 2 }, "c")).toBe(false); +}); // Given a value that isn't an object - an array, a string, a number, // null, or no argument at all // When passed to contains // Then it should throw Error("contains requires an object") // (careful: typeof [] and typeof null are both "object") +test("Given an array, throws an error 'contains requires an object'", () => { + expect(() => contains([])).toThrow("contains requires an object"); +}); diff --git a/Sprint-2/implement/lookup.js b/Sprint-2/implement/lookup.js index a6746e07f..3f57a59f1 100644 --- a/Sprint-2/implement/lookup.js +++ b/Sprint-2/implement/lookup.js @@ -1,5 +1,6 @@ -function createLookup() { - // implementation here +function createLookup(countryCurrencyPairs) { + const lookup = Object.fromEntries(countryCurrencyPairs); + return lookup; } module.exports = createLookup; diff --git a/Sprint-2/implement/lookup.test.js b/Sprint-2/implement/lookup.test.js index 547e06c5a..7c597b17e 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,14 @@ It should return: 'CA': 'CAD' } */ +test("Given an array of arrays representing country code and currency code pairs should return in object", () => { + 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..94bb6190b 100644 --- a/Sprint-2/implement/querystring.js +++ b/Sprint-2/implement/querystring.js @@ -6,7 +6,13 @@ function parseQueryString(queryString) { const keyValuePairs = queryString.split("&"); for (const pair of keyValuePairs) { - const [key, value] = pair.split("="); + if (pair === "") { + continue; + } + const parts = pair.split("="); + + const key = decodeURIComponent(parts[0]); + const value = decodeURIComponent(parts.slice(1).join("=")); queryParams[key] = value; } diff --git a/Sprint-2/implement/querystring.test.js b/Sprint-2/implement/querystring.test.js index 328b8df61..e9e0e9abd 100644 --- a/Sprint-2/implement/querystring.test.js +++ b/Sprint-2/implement/querystring.test.js @@ -3,7 +3,7 @@ // Below are some test cases the implementation doesn't handle well. // Fix the implementation for these tests, 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("should parse values containing '='", () => { expect(parseQueryString("equation=a=b-2")).toEqual({ diff --git a/Sprint-2/implement/tally.js b/Sprint-2/implement/tally.js index f47321812..534f382d7 100644 --- a/Sprint-2/implement/tally.js +++ b/Sprint-2/implement/tally.js @@ -1,3 +1,17 @@ -function tally() {} +function tally(array) { + const lookup = {}; + if (!Array.isArray(array)) { + throw new Error("tally requires an array"); + } + + for (const item of array) { + if (lookup[item]) { + lookup[item] += 1; + } else { + lookup[item] = 1; + } + } + return lookup; +} module.exports = tally; diff --git a/Sprint-2/implement/tally.test.js b/Sprint-2/implement/tally.test.js index ca763c296..c4f7291af 100644 --- a/Sprint-2/implement/tally.test.js +++ b/Sprint-2/implement/tally.test.js @@ -19,16 +19,28 @@ 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 passed an array of items should return object containing count for each", () => { + expect(tally(["a"])).toEqual({ a: 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("When passed an empty array should return 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("When passed an array of duplicate items should return object containing correct count", () => { + expect(tally(["a", "a", "a"])).toEqual({ a: 3 }); +}); // Given an invalid input like a string, a number, or no argument at all // When passed to tally // Then it should throw Error("tally requires an array") +test("WGiven an invalid input should throw error 'tally requires an array'", () => { + expect(() => tally("im a string")).toThrow("tally requires an array"); +}); diff --git a/Sprint-2/interpret/invert.js b/Sprint-2/interpret/invert.js index bb353fb1f..64b2e7dbd 100644 --- a/Sprint-2/interpret/invert.js +++ b/Sprint-2/interpret/invert.js @@ -10,20 +10,29 @@ function invert(obj) { const invertedObj = {}; for (const [key, value] of Object.entries(obj)) { - invertedObj.key = value; + invertedObj[value] = key; } - return invertedObj; } +module.exports = invert; // a) What is the current return value when invert is called with { a : 1 } +// console.log(invert({ a: 1 })); +// It is: { key: 1 } // b) What is the current return value when invert is called with { a: 1, b: 2 } +// console.log(invert({ a: 1, b: 2 })); +// it is: { key: 2 } // c) What is the target return value when invert is called with {a : 1, b: 2} +// it should be : { 1: "a", 2: "b" } // c) What does Object.entries return? Why is it needed in this program? +// it is containing each key/value pair +// it takes an object and turns it into an array // d) Explain why the current return value is different from the target output +// its because of "invertedObj.key = value;" each time it is running its writes +// to the same property of "key" basically overwriting the previous one. // 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..8b8eaaf4c --- /dev/null +++ b/Sprint-2/interpret/invert.test.js @@ -0,0 +1,31 @@ +const invert = require("./invert.js"); + +// inverting original input +test("Inverting original input of 1 property", () => { + expect(invert({ a: 1 })).toEqual({ 1: "a" }); +}); + +// inverting original input of 2 objects and 2 properties +test("Inverting original input of 2 properties", () => { + expect(invert({ a: 1, b: 2 })).toEqual({ 1: "a", 2: "b" }); +}); + +// inverting an empty object +test("Inverting original input correctly", () => { + expect(invert({})).toEqual({}); +}); + +// Inverting an object with an empty string as a value +test("Inverting an object with an empty string as a value", () => { + expect(invert({ a: "" })).toEqual({ "": "a" }); +}); + +// Inverting an object with an empty string as a key +test("Inverting an object with an empty string as a key", () => { + expect(invert({ "": 1 })).toEqual({ 1: "" }); +}); + +// Inverting an object with multiple properties sharing the same value +test("Inverting an object with multiple properties sharing the same value", () => { + expect(invert({ a: 2, b: 2, c: 2 })).toEqual({ 2: "a", 2: "b", 2: "c" }); +});