From b5402d5dd819ed0f8a238b060f7119469abf397f Mon Sep 17 00:00:00 2001 From: Emmanuelgessessew <163059051+Emmanuelgessessew@users.noreply.github.com> Date: Sun, 29 Dec 2024 23:40:02 +0000 Subject: [PATCH 1/3] Fix issues in object property access, iteration, and formatting --- Sprint-2/debug/address.js | 6 +++++- Sprint-2/debug/author.js | 6 +++++- Sprint-2/debug/recipe.js | 8 ++++++-- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/Sprint-2/debug/address.js b/Sprint-2/debug/address.js index 940a6af8..18573278 100644 --- a/Sprint-2/debug/address.js +++ b/Sprint-2/debug/address.js @@ -1,4 +1,7 @@ // Predict and explain first... +// The current code tries to access the houseNumber property of the address object using address[0]. This doesn't work because object properties in JavaScript are typically accessed using dot notation (address.houseNumber) or bracket notation with a string key (address["houseNumber"]). + +// address[0] would only work if address were an array or if there was a property with the key 0 on the object, which isn't the case here. // This code should log out the houseNumber from the address object // but it isn't working... @@ -12,4 +15,5 @@ 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 8c212597..683ddbbc 100644 --- a/Sprint-2/debug/author.js +++ b/Sprint-2/debug/author.js @@ -1,4 +1,8 @@ // Predict and explain first... +// The current code tries to access the houseNumber property of the address object using address[0]. +// This doesn't work because object properties in JavaScript are typically accessed using dot notation (address.houseNumber) or bracket notation with a string key (address["houseNumber"]). +//address[0] would only work if address were an array or if there was a property with the key 0 on the object, which isn't the case here. + // 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 +15,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 6cbdd22c..bebe4b3e 100644 --- a/Sprint-2/debug/recipe.js +++ b/Sprint-2/debug/recipe.js @@ -1,4 +1,8 @@ // Predict and explain first... +// The code attempts to log the ingredients array within the recipe object, but it uses ${recipe} in the string template. +// This will log the string representation of the entire recipe object, which isn't the desired behavior. +//Instead, you want to explicitly log each ingredient on a new line. The code also doesn't include logic to format the ingredients array properly. + // 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 +15,5 @@ const recipe = { }; console.log(`${recipe.title} serves ${recipe.serves} - ingredients: -${recipe}`); +ingredients: +${recipe.ingredients.join("\n")}`); From b34f9de6b81f457992d339f0dc08a6db868fca4b Mon Sep 17 00:00:00 2001 From: Emmanuelgessessew <163059051+Emmanuelgessessew@users.noreply.github.com> Date: Sun, 29 Dec 2024 23:57:09 +0000 Subject: [PATCH 2/3] Implement multiple utility functions with tests: contains, createLookup, parseQueryString, and tally --- Sprint-2/implement/contains.js | 13 ++++-- Sprint-2/implement/contains.test.js | 40 +++++++++++++++++- Sprint-2/implement/lookup.js | 8 +++- Sprint-2/implement/lookup.test.js | 56 +++++++++++++++++++++++++- Sprint-2/implement/querystring.js | 6 ++- Sprint-2/implement/querystring.test.js | 45 +++++++++++++++++++++ Sprint-2/implement/tally.js | 17 ++++++-- Sprint-2/implement/tally.test.js | 56 +++++++++++++++++++++++++- 8 files changed, 225 insertions(+), 16 deletions(-) diff --git a/Sprint-2/implement/contains.js b/Sprint-2/implement/contains.js index cd779308..0833f53d 100644 --- a/Sprint-2/implement/contains.js +++ b/Sprint-2/implement/contains.js @@ -1,3 +1,10 @@ -function contains() {} - -module.exports = contains; +function contains(obj, property) { + + if (typeof obj !== "object" || obj === null || Array.isArray(obj)) { + return false; + } + return Object.prototype.hasOwnProperty.call(obj, property); + } + + module.exports = contains; + \ No newline at end of file diff --git a/Sprint-2/implement/contains.test.js b/Sprint-2/implement/contains.test.js index 326bdb1f..95eb2a34 100644 --- a/Sprint-2/implement/contains.test.js +++ b/Sprint-2/implement/contains.test.js @@ -1,4 +1,4 @@ -const contains = require("./contains.js"); +//const contains = require("./contains.js"); /* Implement a function called contains that checks an object contains a @@ -20,7 +20,7 @@ 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.todo("contains on empty object returns false"); // Given an object with properties // When passed to contains with an existing property name @@ -33,3 +33,39 @@ 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 + + +//answer + +const contains = require("./contains.js"); + +describe("contains", () => { + test("contains on empty object returns false", () => { + expect(contains({}, "a")).toBe(false); + }); + + test("returns true for an object containing the property", () => { + const obj = { a: 1, b: 2 }; + expect(contains(obj, "a")).toBe(true); + expect(contains(obj, "b")).toBe(true); + }); + + test("returns false for an object without the property", () => { + const obj = { a: 1, b: 2 }; + expect(contains(obj, "c")).toBe(false); + }); + + test("returns false for invalid inputs like an array", () => { + expect(contains([1, 2, 3], "a")).toBe(false); + expect(contains(null, "a")).toBe(false); + expect(contains(undefined, "a")).toBe(false); + expect(contains("string", "a")).toBe(false); + }); + + test("handles nested objects correctly", () => { + const obj = { a: { b: 2 }, c: 3 }; + expect(contains(obj, "a")).toBe(true); // "a" exists at the top level + expect(contains(obj.a, "b")).toBe(true); // "b" exists in the nested object + expect(contains(obj, "b")).toBe(false); // "b" doesn't exist at the top level + }); +}); diff --git a/Sprint-2/implement/lookup.js b/Sprint-2/implement/lookup.js index a6746e07..8268b376 100644 --- a/Sprint-2/implement/lookup.js +++ b/Sprint-2/implement/lookup.js @@ -1,5 +1,9 @@ -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 547e06c5..166dfac9 100644 --- a/Sprint-2/implement/lookup.test.js +++ b/Sprint-2/implement/lookup.test.js @@ -1,6 +1,6 @@ -const createLookup = require("./lookup.js"); +// 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,55 @@ It should return: 'CA': 'CAD' } */ + + +//answer +const createLookup = require("./lookup.js"); + +describe("createLookup", () => { + test("creates a country-currency code lookup for multiple codes", () => { + const input = [ + ["US", "USD"], + ["CA", "CAD"], + ["JP", "JPY"], + ]; + const expectedOutput = { + US: "USD", + CA: "CAD", + JP: "JPY", + }; + expect(createLookup(input)).toEqual(expectedOutput); + }); + + test("returns an empty object for an empty array", () => { + expect(createLookup([])).toEqual({}); + }); + + test("handles single pair correctly", () => { + const input = [["GB", "GBP"]]; + const expectedOutput = { GB: "GBP" }; + expect(createLookup(input)).toEqual(expectedOutput); + }); + + test("overwrites duplicate country codes with the last currency", () => { + const input = [ + ["US", "USD"], + ["US", "USN"], + ]; + const expectedOutput = { US: "USN" }; + expect(createLookup(input)).toEqual(expectedOutput); + }); + + test("ignores invalid entries", () => { + const input = [ + ["FR", "EUR"], + ["INVALID"], // invalid entry + ["DE", "EUR"], + ]; + const expectedOutput = { + FR: "EUR", + DE: "EUR", + }; + expect(createLookup(input)).toEqual(expectedOutput); + }); +}); diff --git a/Sprint-2/implement/querystring.js b/Sprint-2/implement/querystring.js index 45ec4e5f..43be9e19 100644 --- a/Sprint-2/implement/querystring.js +++ b/Sprint-2/implement/querystring.js @@ -3,11 +3,13 @@ function parseQueryString(queryString) { if (queryString.length === 0) { return queryParams; } + const keyValuePairs = queryString.split("&"); for (const pair of keyValuePairs) { - const [key, value] = pair.split("="); - queryParams[key] = value; + const [key, ...valueParts] = pair.split("="); + const value = valueParts.join("="); // Handles "=" in the value + queryParams[decodeURIComponent(key)] = decodeURIComponent(value); } return queryParams; diff --git a/Sprint-2/implement/querystring.test.js b/Sprint-2/implement/querystring.test.js index 3e218b78..a89483f6 100644 --- a/Sprint-2/implement/querystring.test.js +++ b/Sprint-2/implement/querystring.test.js @@ -10,3 +10,48 @@ test("parses querystring values containing =", () => { "equation": "x=y+1", }); }); + + test("handles an empty query string", () => { + expect(parseQueryString("")).toEqual({}); + }); + + test("parses multiple key-value pairs", () => { + expect(parseQueryString("a=1&b=2&c=3")).toEqual({ + a: "1", + b: "2", + c: "3", + }); + }); + + test("handles keys without values", () => { + expect(parseQueryString("keyWithoutValue&key2=value2")).toEqual({ + keyWithoutValue: "", + key2: "value2", + }); + }); + + test("decodes URI components", () => { + expect(parseQueryString("name=John%20Doe&city=New%20York")).toEqual({ + name: "John Doe", + city: "New York", + }); + }); + + test("handles keys and values with special characters", () => { + expect(parseQueryString("key=va%26lue&key%2F=another%2Bvalue")).toEqual({ + key: "va&lue", + "key/": "another+value", + }); + }); + + test("handles duplicate keys, keeping the last occurrence", () => { + expect(parseQueryString("a=1&a=2")).toEqual({ + a: "2", + }); + }); + + test("handles values with leading and trailing spaces", () => { + expect(parseQueryString("key= value ")).toEqual({ + key: " value ", + }); + }); diff --git a/Sprint-2/implement/tally.js b/Sprint-2/implement/tally.js index f4732181..e1793789 100644 --- a/Sprint-2/implement/tally.js +++ b/Sprint-2/implement/tally.js @@ -1,3 +1,14 @@ -function tally() {} - -module.exports = tally; +function tally(items) { + if (!Array.isArray(items)) { + throw new Error("Input must be an array"); + } + + const counts = {}; + for (const item of items) { + counts[item] = (counts[item] || 0) + 1; + } + return counts; + } + + module.exports = tally; + \ No newline at end of file diff --git a/Sprint-2/implement/tally.test.js b/Sprint-2/implement/tally.test.js index 2ceffa8d..ea03ea03 100644 --- a/Sprint-2/implement/tally.test.js +++ b/Sprint-2/implement/tally.test.js @@ -1,4 +1,4 @@ -const tally = require("./tally.js"); +//const tally = require("./tally.js"); /** * tally array @@ -23,7 +23,7 @@ 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.todo("tally on an empty array returns an empty object"); // Given an array with duplicate items // When passed to tally @@ -32,3 +32,55 @@ test.todo("tally on an empty array returns an empty object"); // Given an invalid input like a string // When passed to tally // Then it should throw an error + + +//answer +const tally = require("./tally.js"); + +describe("tally", () => { + test("tally on an empty array returns an empty object", () => { + expect(tally([])).toEqual({}); + }); + + test("returns counts for each unique item in an array with duplicate items", () => { + expect(tally(["a", "a", "b", "c"])).toEqual({ + a: 2, + b: 1, + c: 1, + }); + }); + + test("returns counts for a single item array", () => { + expect(tally(["a"])).toEqual({ + a: 1, + }); + }); + + test("handles numbers in the array", () => { + expect(tally([1, 2, 2, 3, 3, 3])).toEqual({ + 1: 1, + 2: 2, + 3: 3, + }); + }); + + test("throws an error for invalid input like a string", () => { + expect(() => tally("invalid input")).toThrow("Input must be an array"); + }); + + test("handles an array with mixed types", () => { + expect(tally([1, "a", 1, "a", "b"])).toEqual({ + 1: 2, + a: 2, + b: 1, + }); + }); + + test("handles special characters", () => { + expect(tally(["!", "!", "?", "a"])).toEqual({ + "!": 2, + "?": 1, + a: 1, + }); + }); +}); From c5a7a70d4ab32e1b502acbfa927e4509511699e3 Mon Sep 17 00:00:00 2001 From: Emmanuelgessessew <163059051+Emmanuelgessessew@users.noreply.github.com> Date: Mon, 30 Dec 2024 00:06:46 +0000 Subject: [PATCH 3/3] Fix invert function to correctly swap keys and values with tests --- Sprint-2/interpret/invert.js | 32 +++++++++++++++++++------------ Sprint-2/interpret/invert.test.js | 23 ++++++++++++++++++++++ 2 files changed, 43 insertions(+), 12 deletions(-) create mode 100644 Sprint-2/interpret/invert.test.js diff --git a/Sprint-2/interpret/invert.js b/Sprint-2/interpret/invert.js index bb353fb1..a5fc707c 100644 --- a/Sprint-2/interpret/invert.js +++ b/Sprint-2/interpret/invert.js @@ -6,24 +6,32 @@ // E.g. invert({x : 10, y : 20}), target output: {"10": "x", "20": "y"} -function invert(obj) { - const invertedObj = {}; - - for (const [key, value] of Object.entries(obj)) { - invertedObj.key = value; - } - - 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" } + +// d) What does Object.entries return? Why is it needed in this program? +//Object.entries({ a: 1, b: 2 }) -// c) What does Object.entries return? Why is it needed in this program? -// 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 +Object.entries({ a: 1, b: 2 }) + +// f) 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; // Correctly assigns the value as a key + } + + return invertedObj; +} -// e) Fix the implementation of invert (and write tests to prove it's fixed!) +module.exports = invert; diff --git a/Sprint-2/interpret/invert.test.js b/Sprint-2/interpret/invert.test.js new file mode 100644 index 00000000..0cb47925 --- /dev/null +++ b/Sprint-2/interpret/invert.test.js @@ -0,0 +1,23 @@ +const invert = require("./invert.js"); + +describe("invert", () => { + test("swaps keys and values for a simple object", () => { + expect(invert({ a: 1, b: 2 })).toEqual({ "1": "a", "2": "b" }); + }); + + test("handles single key-value pair", () => { + expect(invert({ x: 10 })).toEqual({ "10": "x" }); + }); + + test("handles empty objects", () => { + expect(invert({})).toEqual({}); + }); + + test("overwrites duplicate values with last key", () => { + expect(invert({ a: 1, b: 1 })).toEqual({ "1": "b" }); // "b" overwrites "a" + }); + + test("handles non-string keys and values", () => { + expect(invert({ a: true, b: false })).toEqual({ "true": "a", "false": "b" }); + }); +});