diff --git a/Sprint-2/debug/address.js b/Sprint-2/debug/address.js index 940a6af83..8fa05e838 100644 --- a/Sprint-2/debug/address.js +++ b/Sprint-2/debug/address.js @@ -1,4 +1,6 @@ // Predict and explain first... +// I think that it will not run because we're trying to access an object on line 15 +// Instead it should be ${adddress[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..ec96d38f9 100644 --- a/Sprint-2/debug/author.js +++ b/Sprint-2/debug/author.js @@ -1,4 +1,6 @@ // Predict and explain first... +// Because it's trying to iterate through an object +// It should just print author without a 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 +13,4 @@ const author = { alive: true, }; -for (const value of author) { - console.log(value); -} +console.log(Object.values(author)); diff --git a/Sprint-2/debug/recipe.js b/Sprint-2/debug/recipe.js index 6cbdd22cd..c38346ff3 100644 --- a/Sprint-2/debug/recipe.js +++ b/Sprint-2/debug/recipe.js @@ -1,4 +1,6 @@ // Predict and explain first... +// I think it should use recipe.ingredients instead of recipe at line 15 +// Ok I tried it and now I think that it should be done with a loop to list the ingredients // 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 +13,8 @@ const recipe = { }; console.log(`${recipe.title} serves ${recipe.serves} - ingredients: -${recipe}`); +ingredients:`); + +for (const i of recipe.ingredients) { + console.log(i); +} diff --git a/Sprint-2/implement/contains.js b/Sprint-2/implement/contains.js index cd779308a..171187b79 100644 --- a/Sprint-2/implement/contains.js +++ b/Sprint-2/implement/contains.js @@ -1,3 +1,21 @@ -function contains() {} +function contains(toCheck, input) { + if ( + typeof toCheck !== "object" || + Array.isArray(toCheck) || + toCheck === null + ) { + return false; + } + + for (const key in toCheck) { + if (key === input) { + return true; + } + } + + return false; +} + +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..441b870c1 100644 --- a/Sprint-2/implement/contains.test.js +++ b/Sprint-2/implement/contains.test.js @@ -17,10 +17,17 @@ as the object doesn't contains a key of 'c' // When passed an object and a property name // Then it should return true if the object contains the property, false otherwise +test(`Should return true if object contains the property`, () => { + expect(contains({ a: 1, b: 2 }, "a")).toEqual(true); + expect(contains({ b: 2 }, "a")).toEqual(false); +}); + // Given an empty object // When passed to contains // Then it should return false -test.todo("contains on empty object returns false"); +test(`Should return false if object is empty`, () => { + expect(contains({}, "a")).toEqual(false); +}); // Given an object with properties // When passed to contains with an existing property name @@ -33,3 +40,7 @@ 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 + +test(`Should return false if given wron type`, () => { + expect(contains(["a", "b"], "0")).toEqual(false); +}); diff --git a/Sprint-2/implement/lookup.js b/Sprint-2/implement/lookup.js index a6746e07f..f518e2c47 100644 --- a/Sprint-2/implement/lookup.js +++ b/Sprint-2/implement/lookup.js @@ -1,5 +1,22 @@ -function createLookup() { +function createLookup(array) { // implementation here + + if (!Array.isArray(array)) { + throw new Error("Wrong input type, expected array"); + } + + if (array.length === 0) { + throw new Error("Array is empty"); + } + + let output = {}; + for (const pair of array) { + const key = pair[0]; + const value = pair[1]; + + output[key] = value; + } + return output; } module.exports = createLookup; diff --git a/Sprint-2/implement/lookup.test.js b/Sprint-2/implement/lookup.test.js index 547e06c5a..0a2f8758b 100644 --- a/Sprint-2/implement/lookup.test.js +++ b/Sprint-2/implement/lookup.test.js @@ -1,7 +1,5 @@ const createLookup = require("./lookup.js"); -test.todo("creates a country currency code lookup for multiple codes"); - /* Create a lookup object of key value pairs from an array of code pairs @@ -33,3 +31,18 @@ It should return: 'CA': 'CAD' } */ + +test(`Should return an object with correct keys`, () => { + expect( + createLookup([ + ["US", "USD"], + ["CA", "CAD"], + ]) + ).toEqual({ US: "USD", CA: "CAD" }); +}); + +test(`Should throw an error if no input given`, () => { + expect(() => { + createLookup(); + }).toThrow(); +}); diff --git a/Sprint-2/implement/querystring.js b/Sprint-2/implement/querystring.js index 45ec4e5f3..dae044a8b 100644 --- a/Sprint-2/implement/querystring.js +++ b/Sprint-2/implement/querystring.js @@ -1,13 +1,35 @@ function parseQueryString(queryString) { const queryParams = {}; + + // return if input is 0 if (queryString.length === 0) { return queryParams; } + + // replace all + by ' ' + queryString = queryString.replaceAll("+", " "); + + // split by & const keyValuePairs = queryString.split("&"); for (const pair of keyValuePairs) { - const [key, value] = pair.split("="); - queryParams[key] = value; + if (pair.length > 0) { + const parts = pair.split("="); + // first part as the key + const key = decodeURIComponent(parts[0]); + // second to last parts as the value + const value = decodeURIComponent(parts.slice(1).join("=")); + + // if no key then assign new value + if (!Object.hasOwn(queryParams, key)) { + queryParams[key] = value; + } else if (Array.isArray(queryParams[key])) { + queryParams[key].push(value); + // if not array then turn into array with old and new values + } else { + queryParams[key] = [queryParams[key], value]; + } + } } return queryParams; diff --git a/Sprint-2/implement/tally.js b/Sprint-2/implement/tally.js index f47321812..70728789c 100644 --- a/Sprint-2/implement/tally.js +++ b/Sprint-2/implement/tally.js @@ -1,3 +1,23 @@ -function tally() {} +function tally(input) { + let out = {}; + + if (!Array.isArray(input)) { + throw new Error( + `Wrong input type, expected an array but got ${typeof input}` + ); + } + + for (const i of input) { + if (!Object.hasOwn(out, i)) { + out[i] = 1; + } else { + out[i] = out[i] + 1; + } + } + + return out; +} + +console.log(tally(["a", "a", "b"])); module.exports = tally; diff --git a/Sprint-2/implement/tally.test.js b/Sprint-2/implement/tally.test.js index 2ceffa8dd..9b1df2622 100644 --- a/Sprint-2/implement/tally.test.js +++ b/Sprint-2/implement/tally.test.js @@ -23,12 +23,23 @@ 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(`Should an empty object when empty array is passed`, () => { + expect(tally([])).toEqual({}); +}); // Given an array with duplicate items // When passed to tally // Then it should return counts for each unique item +test(`Should return counts for each unique item`, () => { + expect(tally(["a", "a", "b"])).toEqual({ a: 2, b: 1 }); + expect(tally(["toString", "a", "b"])).toEqual({ toString: 1, a: 1, b: 1 }); +}); // Given an invalid input like a string // When passed to tally // Then it should throw an error +test(`Should return an error when given wrong input type`, () => { + expect(() => { + tally("hello"); + }).toThrow(); +}); diff --git a/Sprint-2/interpret/invert.js b/Sprint-2/interpret/invert.js index bb353fb1f..eabee4a8c 100644 --- a/Sprint-2/interpret/invert.js +++ b/Sprint-2/interpret/invert.js @@ -10,20 +10,28 @@ 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 } +// { 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(obj) returns an array of [key, value] pairs, one small array for each property in the object // d) Explain why the current return value is different from the target output +// because the key and value wasn't inverted in the code // e) Fix the implementation of invert (and write tests to prove it's fixed!) +// ok! diff --git a/Sprint-2/interpret/invert.test.js b/Sprint-2/interpret/invert.test.js new file mode 100644 index 000000000..35d7673b2 --- /dev/null +++ b/Sprint-2/interpret/invert.test.js @@ -0,0 +1,30 @@ +const invert = require("./invert.js"); + +describe("invert", () => { + test("should swap keys and values for a simple object", () => { + expect(invert({ x: 10, y: 20 })).toEqual({ 10: "x", 20: "y" }); + }); + + test("should work with a single key-value pair", () => { + expect(invert({ a: 1 })).toEqual({ 1: "a" }); + }); + + test("should return an empty object when given an empty object", () => { + expect(invert({})).toEqual({}); + }); + + test("should overwrite earlier keys when two values are the same", () => { + // if two keys share the same value, the later key wins after inversion + expect(invert({ a: 1, b: 1 })).toEqual({ 1: "b" }); + }); + + test("should coerce numeric and boolean values to string keys", () => { + expect(invert({ a: true, b: false })).toEqual({ true: "a", false: "b" }); + }); + + test("should not mutate the original object", () => { + const original = { a: 1, b: 2 }; + invert(original); + expect(original).toEqual({ a: 1, b: 2 }); + }); +});