diff --git a/Sprint-2/debug/address.js b/Sprint-2/debug/address.js index 940a6af83..59669c771 100644 --- a/Sprint-2/debug/address.js +++ b/Sprint-2/debug/address.js @@ -13,3 +13,23 @@ const address = { }; console.log(`My house number is ${address[0]}`); + +//my prediction is "undefined" because objects use names. When we js executes this code it is seeing address[0], it looks +//for 0 in objects, won't find it and will return , undefined. + +// i have run the code in devtools and got the "My house number is undefined" message. + +//the fixed code + +const address = { + houseNumber: 42, + street: "Imaginary Road", + city: "Manchester", + country: "England", + postcode: "XYZ 123", +}; + +console.log(`My house number is ${address.houseNumber}`); + + + diff --git a/Sprint-2/debug/author.js b/Sprint-2/debug/author.js index 8c2125977..33d9273f7 100644 --- a/Sprint-2/debug/author.js +++ b/Sprint-2/debug/author.js @@ -14,3 +14,19 @@ const author = { for (const value of author) { console.log(value); } + +// this code will throw an error because on line for 14 it is trying to loop a plain object. + +const author = { + firstName: "Zadie", + lastName: "Smith", + occupation: "writer", + age: 40, + alive: true, +}; + +for (const value of Object.values(author)) { + console.log(value); +} + +//we fix this by using Object.values, i.e. turning plain object in an array. diff --git a/Sprint-2/debug/recipe.js b/Sprint-2/debug/recipe.js index 6cbdd22cd..933ae093e 100644 --- a/Sprint-2/debug/recipe.js +++ b/Sprint-2/debug/recipe.js @@ -13,3 +13,19 @@ const recipe = { console.log(`${recipe.title} serves ${recipe.serves} ingredients: ${recipe}`); + +//Prediction: +//output will be, [object Object]. Because recipe is an object, when js converts objects to a string (template literals can only insert strings) it calls on toString() method, which +//by default returns [Object Object]. We can fix this code by using join(\n) method. + +//fixed code: + +const recipe = { + title: "bruschetta", + serves: 2, + ingredients: ["olive oil", "tomatoes", "salt", "pepper"], +}; + +console.log(`${recipe.title} serves ${recipe.serves} +Ingredients: +${recipe.ingredients.join("\n")}`); diff --git a/Sprint-2/implement/contains.js b/Sprint-2/implement/contains.js index cd779308a..f8d9f79ee 100644 --- a/Sprint-2/implement/contains.js +++ b/Sprint-2/implement/contains.js @@ -1,3 +1,9 @@ -function contains() {} +function contains(obj, property) { + if (typeof obj !== "object" || obj === null || Array.isArray(obj)) { + return false; + } + + return Object.hasOwn(obj, property); +} module.exports = contains; diff --git a/Sprint-2/implement/contains.test.js b/Sprint-2/implement/contains.test.js index 326bdb1f2..0097d8d69 100644 --- a/Sprint-2/implement/contains.test.js +++ b/Sprint-2/implement/contains.test.js @@ -17,19 +17,47 @@ 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("returns true if the object contains the property", () => { + const object = { a: 1, b: 2 }; + + expect(contains(object, "a")).toBe(true); +}); + +test("returns false if the object does not contain the property", () => { + const object = { a: 1, b: 2 }; + + expect(contains(object, "c")).toBe(false); +}); + // 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 when property exists", () => { + 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 property does not exist", () => { + 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 an array", () => { + expect(contains(["a", "b"], "0")).toBe(false); +}); diff --git a/Sprint-2/implement/lookup.js b/Sprint-2/implement/lookup.js index a6746e07f..da5b177d4 100644 --- a/Sprint-2/implement/lookup.js +++ b/Sprint-2/implement/lookup.js @@ -1,5 +1,11 @@ -function createLookup() { - // implementation here +function createLookup(countryCurrencyPairs) { + const lookup = {}; + + for (const [countryCode, currencyCode] of countryCurrencyPairs) { + lookup[countryCode] = currencyCode; + } + + return lookup; } module.exports = createLookup; diff --git a/Sprint-2/implement/lookup.test.js b/Sprint-2/implement/lookup.test.js index 547e06c5a..a8ad2bbd2 100644 --- a/Sprint-2/implement/lookup.test.js +++ b/Sprint-2/implement/lookup.test.js @@ -1,6 +1,19 @@ 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 countryCurrencyPairs = [ + ["US", "USD"], + ["CA", "CAD"], + ["GB", "GBP"], + ]; + const result = createLookup(countryCurrencyPairs); + + expect(result).toEqual({ + US: "USD", + CA: "CAD", + GB: "GBP", + }); +}); /* diff --git a/Sprint-2/implement/querystring.js b/Sprint-2/implement/querystring.js index 45ec4e5f3..32daa79af 100644 --- a/Sprint-2/implement/querystring.js +++ b/Sprint-2/implement/querystring.js @@ -1,13 +1,43 @@ function parseQueryString(queryString) { const queryParams = {}; - if (queryString.length === 0) { + + if (queryString === "") { return queryParams; } + const keyValuePairs = queryString.split("&"); for (const pair of keyValuePairs) { - const [key, value] = pair.split("="); - queryParams[key] = value; + + if (pair === "") continue; + + + const equalsIndex = pair.indexOf("="); + + let key, value; + + if (equalsIndex === -1) { + key = pair; + value = ""; + } else { + key = pair.slice(0, equalsIndex); + value = pair.slice(equalsIndex + 1); + } + + + key = decodeURIComponent(key.replace(/\+/g, " ")); + value = decodeURIComponent(value.replace(/\+/g, " ")); + + + if (queryParams.hasOwnProperty(key)) { + if (Array.isArray(queryParams[key])) { + queryParams[key].push(value); + } else { + queryParams[key] = [queryParams[key], value]; + } + } else { + queryParams[key] = value; + } } return queryParams; diff --git a/Sprint-2/implement/querystring.test.js b/Sprint-2/implement/querystring.test.js index 328b8df61..8f2ce6823 100644 --- a/Sprint-2/implement/querystring.test.js +++ b/Sprint-2/implement/querystring.test.js @@ -37,12 +37,23 @@ test("should replace '+' by ' '", () => { }); }); -// Stretch exercise: Handling query strings that contain identical keys +test("returns empty object for empty query", () => { + expect(parseQueryString("")).toEqual({}); +}); + +test("ignores multiple empty pairs", () => { + expect(parseQueryString("&&&&")).toEqual({}); +}); -// Delete this test if you are not working on this optional case -test("should store values of a key in an array when the key has 2 or more values", () => { - expect(parseQueryString("key=value1&key=value2&key=value3&foo=bar")).toEqual({ - key: ["value1", "value2", "value3"], - foo: "bar", +test("decodes encoded spaces", () => { + expect(parseQueryString("name=John%20Doe")).toEqual({ + name: "John Doe", }); }); + +test("keeps all '=' in values", () => { + expect(parseQueryString("expr=a=b=c=d")).toEqual({ + expr: "a=b=c=d", + }); +}); + diff --git a/Sprint-2/implement/tally.js b/Sprint-2/implement/tally.js index f47321812..68af13a7a 100644 --- a/Sprint-2/implement/tally.js +++ b/Sprint-2/implement/tally.js @@ -1,3 +1,19 @@ -function tally() {} +function tally(items) { + if (!Array.isArray(items)) { + throw new Error("Input must be an array"); + } + + const counts = {}; + + for (const item of items) { + if (counts[item]) { + counts[item]++; + } else { + counts[item] = 1; + } + } + + return counts; +} module.exports = tally; diff --git a/Sprint-2/implement/tally.test.js b/Sprint-2/implement/tally.test.js index 2ceffa8dd..403b3fdd1 100644 --- a/Sprint-2/implement/tally.test.js +++ b/Sprint-2/implement/tally.test.js @@ -20,15 +20,40 @@ const tally = require("./tally.js"); // When passed an array of items // Then it should return an object containing the count for each unique item +test("returns an object containing the count for each unique item", () => { + expect(tally([1, 2, 2, 3, 3, 3])).toEqual({ + 1: 1, + 2: 2, + 3: 3, + }); +}); + // 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 duplicate items", () => { + 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 an error for invalid input", () => { + expect(() => tally("abc")).toThrow(); +}); + diff --git a/Sprint-2/interpret/invert.js b/Sprint-2/interpret/invert.js index bb353fb1f..853ae6a9f 100644 --- a/Sprint-2/interpret/invert.js +++ b/Sprint-2/interpret/invert.js @@ -18,12 +18,61 @@ function invert(obj) { // 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() returns an array of an object's key-value pairs, and it is needed in this program +//so the for...of loop can access both each key and its corresponding value to swap them. + // d) Explain why the current return value is different from the target output +//It is different because invertObj.key creates a property literally calley "key". If we want to use the value of +//a variable as the property name, (invertedObj[value] = key) must be used instead. + // e) Fix the implementation of invert (and write tests to prove it's fixed!) + +function invert(obj) { + const invertedObj = {}; + + for (const [key, value] of Object.entries(obj)) { + invertedObj[value] = key; + } + + return invertedObj; +} + + + +describe("invert", () => { + test("inverts an empty object", () => { + expect(invert({})).toEqual({}); + }); + + test("inverts a single property", () => { + expect(invert({ a: 1 })).toEqual({ 1: "a" }); + }); + + test("inverts multiple properties", () => { + expect(invert({ a: 1, b: 2 })).toEqual({ + 1: "a", + 2: "b", + }); + }); + + test("works with other values", () => { + expect(invert({ x: 10, y: 20 })).toEqual({ + 10: "x", + 20: "y", + }); + }); +}); +