diff --git a/Sprint-2/debug/address.js b/Sprint-2/debug/address.js index 940a6af83..d5d562082 100644 --- a/Sprint-2/debug/address.js +++ b/Sprint-2/debug/address.js @@ -1,4 +1,7 @@ // Predict and explain first... +//The issue with this program is that console.log is logging the address but is an object. address[0] only works for arrays. +//As address is an object, we must use a key rather than numeric indexes. +//Also there is no property called "0" inside address. // This code should log out the houseNumber from the address object // but it isn't working... @@ -12,4 +15,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..3de90cec5 100644 --- a/Sprint-2/debug/author.js +++ b/Sprint-2/debug/author.js @@ -1,4 +1,7 @@ // Predict and explain first... +// The issue with this program is that for.. of only works on iterable objects. +// Plain JavaScript objects are not iterable, so this will throw a "TypeError". +// To fix this, we can use Object.values(author) which converts the object's value into an array. // 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 +14,6 @@ const author = { alive: true, }; -for (const value of author) { +for (const value of Object.values(author)) { console.log(value); -} +} \ No newline at end of file diff --git a/Sprint-2/debug/recipe.js b/Sprint-2/debug/recipe.js index 6cbdd22cd..f39b7253b 100644 --- a/Sprint-2/debug/recipe.js +++ b/Sprint-2/debug/recipe.js @@ -1,4 +1,9 @@ // Predict and explain first... +//The issue with this program is that ${recipe} inserts the entire object into the template string. +// When an object is converted to a string, it becomes "[object Object]". +//Instead we would need to access the ingredients array. +//We can loop through recipe.ingredients or use join("\n") +//This will print each ingredients on a new line. // This program should log out the title, how many it serves and the ingredients. // Each ingredient should be logged on a new line @@ -11,5 +16,5 @@ const recipe = { }; console.log(`${recipe.title} serves ${recipe.serves} - ingredients: -${recipe}`); +ingredients: +${recipe.ingredients.join("\n")}`); \ No newline at end of file diff --git a/Sprint-2/implement/contains.js b/Sprint-2/implement/contains.js index cd779308a..e6df0318b 100644 --- a/Sprint-2/implement/contains.js +++ b/Sprint-2/implement/contains.js @@ -1,3 +1,10 @@ -function contains() {} - +function contains(object, propertyName) { +if ( + typeof object !== "object" || + object === null || + Array.isArray(object)) { + return false; + } + return propertyName in object; +} module.exports = contains; diff --git a/Sprint-2/implement/contains.test.js b/Sprint-2/implement/contains.test.js index 326bdb1f2..8ec0e6ded 100644 --- a/Sprint-2/implement/contains.test.js +++ b/Sprint-2/implement/contains.test.js @@ -20,16 +20,27 @@ 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("returns false for empty object", () => { + 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("returns true when object contains property", () => { + 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("returns false when object does not contain property", () => { + expect(contains({ a: 1, b: 2 }, "c")).toBe(false); +}); // Given invalid parameters like an array // When passed to contains // Then it should return false or throw an error +test("returns false for invalid input (array)", () => { + expect(contains(["a", "b"], "a")).toBe(false); +}); \ No newline at end of file diff --git a/Sprint-2/implement/lookup.js b/Sprint-2/implement/lookup.js index a6746e07f..b5414458a 100644 --- a/Sprint-2/implement/lookup.js +++ b/Sprint-2/implement/lookup.js @@ -1,5 +1,12 @@ -function createLookup() { - // implementation here +function createLookup(pairs) { +const result = {}; +for (const pair of pairs) { + const countryCode = pair[0]; + const currencyCode = pair[1]; + result[countryCode] = currencyCode; +} + +return result; } module.exports = createLookup; diff --git a/Sprint-2/implement/lookup.test.js b/Sprint-2/implement/lookup.test.js index 547e06c5a..8a44c9655 100644 --- a/Sprint-2/implement/lookup.test.js +++ b/Sprint-2/implement/lookup.test.js @@ -1,6 +1,12 @@ 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", () => { + const input = [['US', 'USD'], ['CA', 'CAD']]; + expect(createLookup(input)).toEqual({ + US: 'USD', + CA: 'CAD' + }); +}); /* diff --git a/Sprint-2/implement/querystring.js b/Sprint-2/implement/querystring.js index 45ec4e5f3..a9de58140 100644 --- a/Sprint-2/implement/querystring.js +++ b/Sprint-2/implement/querystring.js @@ -1,12 +1,22 @@ function parseQueryString(queryString) { const queryParams = {}; + if (queryString.length === 0) { return queryParams; } + const keyValuePairs = queryString.split("&"); for (const pair of keyValuePairs) { - const [key, value] = pair.split("="); + const equalsIndex = pair.indexOf("="); + + if (equalsIndex === -1) { + queryParams[pair] = ""; + continue; + } + const key = pair.slice(0, equalsIndex); + const value = pair.slice(equalsIndex + 1); + queryParams[key] = value; } diff --git a/Sprint-2/implement/querystring.test.js b/Sprint-2/implement/querystring.test.js index 3e218b789..c034de5fa 100644 --- a/Sprint-2/implement/querystring.test.js +++ b/Sprint-2/implement/querystring.test.js @@ -10,3 +10,23 @@ test("parses querystring values containing =", () => { "equation": "x=y+1", }); }); + +// Empty Input +test("returns empty object for empty string", () => { + expect(parseQueryString("")).toEqual({}); +}); + +// Normal single pair +test("parses a single key/value pair", () => { + expect(parseQueryString("a=1")).toEqual({ a: "1" }); +}); + +// Normal multiple pairs +test("parses multiple key/value pairs", () => { + expect(parseQueryString("a=1&b=2")).toEqual({ a: "1", b: "2" }); +}); + +// Empty value should be an empty string +test("handles a key with an empty value", () => { + expect(parseQueryString("a=")).toEqual({ a: "" }); +}); \ No newline at end of file diff --git a/Sprint-2/implement/tally.js b/Sprint-2/implement/tally.js index f47321812..7f9920847 100644 --- a/Sprint-2/implement/tally.js +++ b/Sprint-2/implement/tally.js @@ -1,3 +1,17 @@ -function tally() {} +function tally(items) { + if (!Array.isArray(items)){ + throw new Error("Input must be an array"); + } + const result = {}; + + for (const item of items) { + if (result[item]) { + result[item] += 1; + } else { + result[item] = 1; + } + } + return result; + } module.exports = tally; diff --git a/Sprint-2/implement/tally.test.js b/Sprint-2/implement/tally.test.js index 2ceffa8dd..811780f35 100644 --- a/Sprint-2/implement/tally.test.js +++ b/Sprint-2/implement/tally.test.js @@ -23,12 +23,24 @@ 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("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("counts frequency of items in array", () => { + expect(tally(['a', 'a', 'b', 'c'])).toEqual({ + a: 2, + b: 1, + c: 1 + }); +}); // Given an invalid input like a string // When passed to tally // Then it should throw an error +test("throws error for invalid input", () => { + expect(() => tally("abc")).toThrow(); +}); \ No newline at end of file diff --git a/Sprint-2/interpret/invert.js b/Sprint-2/interpret/invert.js index bb353fb1f..18f688515 100644 --- a/Sprint-2/interpret/invert.js +++ b/Sprint-2/interpret/invert.js @@ -10,20 +10,31 @@ 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 } +// The current return value when invert is called with { a : 1 } is { key: 1 } // b) What is the current return value when invert is called with { a: 1, b: 2 } +// When calling invert({ a: 1, b: 2 }); the loop runs twice, the first iteration outputs invertedObj.key = 1l +// The second iterations overwrites the first, so the current value returned is { key: 2 } // c) What is the target return value when invert is called with {a : 1, b: 2} +// The target output should swap keys and value, therefore the output (using Node REPL) after the fix is { '1': 'a', '2': 'b' } +// After fixing the bug in the code. The values become keys, and the keys become the values. +// Object keys are stored as strings. -// c) What does Object.entries return? Why is it needed in this program? +// d) What does Object.entries return? Why is it needed in this program? +// Object.entries({ a: 1, b: 2}) return [["a", 1]], ["b", 2]] +// It is needed because objects are not iterable with for...of, but the entries array is, so we can loop key/value pairs. -// d) Explain why the current return value is different from the target output +// e) Explain why the current return value is different from the target output +// In the code (before the bug fix) invertedObj.key = value creates a literal property called "key" each time, overwriting it. +// It does not use the variable key/value for dynamic property names, so it can't swap keys and values correctly. -// e) Fix the implementation of invert (and write tests to prove it's fixed!) +// f) 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..5e678a493 --- /dev/null +++ b/Sprint-2/interpret/invert.test.js @@ -0,0 +1,16 @@ +const invert = require("./invert.js"); + +test("inverts a single key value pair", () => { + expect(invert({ a: 1 })).toEqual({ "1": "a" }); +}); + +test("inverts multiple key value pairs", () => { + expect(invert({ a: 1, b: 2 })).toEqual({ + "1": "a", + "2": "b" + }); +}); + +test("returns empty object when given empty object", () => { + expect(invert({})).toEqual({}); +}); \ No newline at end of file