diff --git a/Sprint-2/debug/address.js b/Sprint-2/debug/address.js index 940a6af83..d9fc08b5a 100644 --- a/Sprint-2/debug/address.js +++ b/Sprint-2/debug/address.js @@ -12,4 +12,6 @@ const address = { postcode: "XYZ 123", }; -console.log(`My house number is ${address[0]}`); +console.log(`My house number is ${address.houseNumber}`); + +//address is an object without a key, therefore address[0] is undefined. To fix this we write "address.houseNumber". \ No newline at end of file diff --git a/Sprint-2/debug/author.js b/Sprint-2/debug/author.js index 8c2125977..3df469f06 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 in author) { console.log(value); } +// for...of works only with iterable objects like arrays, strings, maps..etc. In order to fix this problem we can use "for...in" to loop through the keys of the object. \ No newline at end of file diff --git a/Sprint-2/debug/recipe.js b/Sprint-2/debug/recipe.js index 6cbdd22cd..76714b2c7 100644 --- a/Sprint-2/debug/recipe.js +++ b/Sprint-2/debug/recipe.js @@ -10,6 +10,9 @@ const recipe = { ingredients: ["olive oil", "tomatoes", "salt", "pepper"], }; -console.log(`${recipe.title} serves ${recipe.serves} - ingredients: -${recipe}`); + +console.log(`${recipe.title} serves ${recipe.serves} ingredients:`); +for(let ingredient of recipe.ingredients) { + console.log(ingredient) +} +//I used a for...of loop to list each ingredient on a new line. \ No newline at end of file diff --git a/Sprint-2/implement/contains.js b/Sprint-2/implement/contains.js index cd779308a..d3c9971df 100644 --- a/Sprint-2/implement/contains.js +++ b/Sprint-2/implement/contains.js @@ -1,3 +1,8 @@ -function contains() {} +function contains(obj, propName) { + if (typeof obj !== "object" || obj === null || Array.isArray(obj)) { + return false; + } + return propName in obj; +} module.exports = contains; diff --git a/Sprint-2/implement/contains.test.js b/Sprint-2/implement/contains.test.js index 326bdb1f2..204564de4 100644 --- a/Sprint-2/implement/contains.test.js +++ b/Sprint-2/implement/contains.test.js @@ -16,20 +16,39 @@ 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 +describe("contains", () => { + test("returns true if the object contains the property, false otherwise", () => { + expect(contains({CYF:3,ITP:5}, "ITP")).toBe(true); + }) // 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", () => { + 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 for an existing property", () => { + expect(contains({ a: 1, b: 2 }, "a")).toBe(true); + expect(contains({ x: 10, y: 20 }, "y")).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 for a non-existent property", () => { + expect(contains({ a: 1, b: 2 }, "c")).toBe(false); + expect(contains({ x: 10, y: 20 }, "z")).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 parameters like an array", () => { + expect(contains([], "a")).toBe(false); + expect(contains(null, "a")).toBe(false); + expect(contains("string", "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..3ed07e988 100644 --- a/Sprint-2/implement/lookup.js +++ b/Sprint-2/implement/lookup.js @@ -1,5 +1,9 @@ -function createLookup() { - // implementation here +function createLookup(pairs) { + const lookup = {}; + for (const [country, currency] of pairs) { + lookup[country] = currency + } + return lookup; } module.exports = createLookup; diff --git a/Sprint-2/implement/lookup.test.js b/Sprint-2/implement/lookup.test.js index 547e06c5a..a0f6e17e2 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", () => { + const input = [['US', 'USD'], ['CA', 'CAD'], ['JP', 'JPY']]; + const expected = { + US: 'USD', + CA: 'CAD', + JP: 'JPY' + }; + + expect(createLookup(input)).toEqual(expected); + }); + /* diff --git a/Sprint-2/implement/querystring.js b/Sprint-2/implement/querystring.js index 45ec4e5f3..84b6a41fa 100644 --- a/Sprint-2/implement/querystring.js +++ b/Sprint-2/implement/querystring.js @@ -6,7 +6,9 @@ function parseQueryString(queryString) { const keyValuePairs = queryString.split("&"); for (const pair of keyValuePairs) { - const [key, value] = pair.split("="); + const index = pair.indexOf("="); + const key = pair.slice(0, index); + const value = pair.slice(index + 1); queryParams[key] = value; } diff --git a/Sprint-2/implement/querystring.test.js b/Sprint-2/implement/querystring.test.js index 3e218b789..7dae339b2 100644 --- a/Sprint-2/implement/querystring.test.js +++ b/Sprint-2/implement/querystring.test.js @@ -5,8 +5,22 @@ const parseQueryString = require("./querystring.js") -test("parses querystring values containing =", () => { - expect(parseQueryString("equation=x=y+1")).toEqual({ - "equation": "x=y+1", +describe("querystring parser", () => { + test("parses querystring values containing =", () => { + expect(parseQueryString("equation=x=y+1")).toEqual({ + "equation": "x=y+1", + }); }); -}); + test("returns empty object for empty query string", () => { + expect(parseQueryString("")).toEqual({}); + }); + test("handles key with missing value", () => { + expect(parseQueryString("key=")).toEqual({ key: "" }); + }); + test("handles missing key with value", () => { + expect(parseQueryString("=value")).toEqual({ "": "value" }); + }); + test("handles multiple key-value pairs", () => { + expect(parseQueryString("a=1&b=2")).toEqual({ a: "1", b: "2" }); + }); + }) \ No newline at end of file diff --git a/Sprint-2/implement/tally.js b/Sprint-2/implement/tally.js index f47321812..c3192d498 100644 --- a/Sprint-2/implement/tally.js +++ b/Sprint-2/implement/tally.js @@ -1,3 +1,17 @@ -function tally() {} +function tally(arr) { + if (!Array.isArray(arr)) { + throw new Error("Input must be an array"); + } + + const result = {}; + for (const item of arr) { + if (result[item] === undefined) { + 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..19267fa3f 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 - +describe("tally test", () => { +test("tally counts each unique item correctly", () => { + expect(tally(["a", "b", "c"])).toEqual({ a: 1, b: 1, c: 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("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("tally counts each unique item correctly", () => { + 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("tally throws an error for invalid input", () => { + expect(() => tally("CYF")).toThrow("Input must be an array"); +}); +}); \ No newline at end of file diff --git a/Sprint-2/interpret/invert.js b/Sprint-2/interpret/invert.js index bb353fb1f..2f5f84497 100644 --- a/Sprint-2/interpret/invert.js +++ b/Sprint-2/interpret/invert.js @@ -10,20 +10,34 @@ 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 } - +//{ 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 gives an array of [key, value] pairs. // d) Explain why the current return value is different from the target output - +// invertedObj.key sets a property literally called "key". We need to use the variable key as the dynamic property name. and we need to change "key" to "value" and "value" to "key". // e) Fix the implementation of invert (and write tests to prove it's fixed!) +console.assert( + JSON.stringify(invert({ a: 1, b: 2 })) === JSON.stringify({ "1": "a", "2": "b" }), + "multiple keys test failed" +); +console.assert( + JSON.stringify(invert({ x: "hello", y: "world" })) === JSON.stringify({ "hello": "x", "world": "y" }), + "string cases test failed" +); +console.assert( + JSON.stringify(invert({})) === JSON.stringify({}), + "empty test failed" +); +console.log("All tests passed!");