diff --git a/Sprint-2/debug/address.js b/Sprint-2/debug/address.js index 940a6af83..ed3c559c1 100644 --- a/Sprint-2/debug/address.js +++ b/Sprint-2/debug/address.js @@ -1,9 +1,11 @@ // Predict and explain first... - +// I think, for object we have to use key as property value instead of array index in console.log. // This code should log out the houseNumber from the address object // but it isn't working... // Fix anything that isn't working +//Correct Code: + const address = { houseNumber: 42, street: "Imaginary Road", @@ -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..ecb3a1e0c 100644 --- a/Sprint-2/debug/author.js +++ b/Sprint-2/debug/author.js @@ -1,5 +1,6 @@ // Predict and explain first... - +// I think we have to use the dot notation associated with the key with variable author to see the property values as output. +// Besides, we can omit for...of loop for this time cause there is no function for this program. // 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 +12,10 @@ const author = { alive: true, }; -for (const value of author) { - console.log(value); -} + console.log(author.firstName); + console.log(author.lastName); + console.log(author.occupation); + console.log(author.age); + console.log(author.alive); + + diff --git a/Sprint-2/debug/recipe.js b/Sprint-2/debug/recipe.js index 6cbdd22cd..6ff683eb4 100644 --- a/Sprint-2/debug/recipe.js +++ b/Sprint-2/debug/recipe.js @@ -1,5 +1,6 @@ // Predict and explain first... - +// My prediction is for log out the title, how many it serves and the ingridients we have to use several console.log for each of the property +// value. Besides, for each ingredient should be logged on a new line, we can use array index for the ingredients array. // This program should log out the title, how many it serves and the ingredients. // Each ingredient should be logged on a new line // How can you fix it? @@ -10,6 +11,10 @@ const recipe = { ingredients: ["olive oil", "tomatoes", "salt", "pepper"], }; -console.log(`${recipe.title} serves ${recipe.serves} - ingredients: -${recipe}`); + +console.log(recipe.title); +console.log(recipe.serves); +console.log(recipe.ingredients[0]); +console.log(recipe.ingredients[1]); +console.log(recipe.ingredients[2]); +console.log(recipe.ingredients[3]); \ No newline at end of file diff --git a/Sprint-2/implement/contains.js b/Sprint-2/implement/contains.js index cd779308a..a07485828 100644 --- a/Sprint-2/implement/contains.js +++ b/Sprint-2/implement/contains.js @@ -1,3 +1,6 @@ -function contains() {} +function contains(input, propertyName) { + const result = Object.hasOwn(input, propertyName); + return result; +} module.exports = contains; diff --git a/Sprint-2/implement/contains.test.js b/Sprint-2/implement/contains.test.js index 326bdb1f2..4970c9227 100644 --- a/Sprint-2/implement/contains.test.js +++ b/Sprint-2/implement/contains.test.js @@ -1,35 +1,42 @@ const contains = require("./contains.js"); -/* -Implement a function called contains that checks an object contains a -particular property - -E.g. contains({a: 1, b: 2}, 'a') // returns true -as the object contains a key of 'a' - -E.g. contains({a: 1, b: 2}, 'c') // returns false -as the object doesn't contains a key of 'c' -*/ - -// Acceptance criteria: - -// 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 - -// Given an empty object -// When passed to contains -// Then it should return false -test.todo("contains on empty object returns false"); - -// Given an object with properties -// When passed to contains with an existing property name -// Then it should return true - -// Given an object with properties -// When passed to contains with a non-existent property name -// Then it should return false - -// Given invalid parameters like an array -// When passed to contains -// Then it should return false or throw an error +test("contains on empty object returns false", function () { + const object = {}; + const currentOutput = contains(object, "a"); + const targetOutput = false; + + expect(currentOutput).toBe(targetOutput); +}); + +test("contains with an existing property name should return true", function () { + const object = { + a: 1, + b: 2, + flower: "red", + }; + const currentOutput = contains(object, "a"); + const targetOutput = true; + + expect(currentOutput).toBe(targetOutput); +}); + +test("contains with a non-existing property name should return false", function () { + const object = { + a: 1, + b: 2, + flower: "red", + }; + const currentOutput = contains(object, "c"); + const targetOutput = false; + + expect(currentOutput).toBe(targetOutput); +}); + +test("contains with an array should return false", function () { + const object = ["a", "b", "c"]; + + const currentOutput = contains(object, "c"); + const targetOutput = false; + + expect(currentOutput).toBe(targetOutput); +}); diff --git a/Sprint-2/implement/lookup.js b/Sprint-2/implement/lookup.js index a6746e07f..2dfabaaa4 100644 --- a/Sprint-2/implement/lookup.js +++ b/Sprint-2/implement/lookup.js @@ -1,5 +1,6 @@ -function createLookup() { - // implementation here +function createLookup(countryCurrencyPairs) { + const keyValuePairs = Object.fromEntries(countryCurrencyPairs); + return keyValuePairs; } module.exports = createLookup; diff --git a/Sprint-2/implement/lookup.test.js b/Sprint-2/implement/lookup.test.js index 547e06c5a..8bbff3ea9 100644 --- a/Sprint-2/implement/lookup.test.js +++ b/Sprint-2/implement/lookup.test.js @@ -1,35 +1,17 @@ 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 - -Acceptance Criteria: - -Given - - An array of arrays representing country code and currency code pairs - e.g. [['US', 'USD'], ['CA', 'CAD']] - -When - - createLookup function is called with the country-currency array as an argument - -Then - - It should return an object where: - - The keys are the country codes - - The values are the corresponding currency codes - -Example -Given: [['US', 'USD'], ['CA', 'CAD']] - -When -createLookup(countryCurrencyPairs) is called - -Then -It should return: - { - 'US': 'USD', - 'CA': 'CAD' - } -*/ +test("creates a country currency code lookup for multiple codes", function () { + const arr = [ + ["US", "USD"], + ["CA", "CAD"], + ["BD", "BDT"], + ]; + const currentOutput = createLookup(arr); + const targetOutput = { + US: "USD", + CA: "CAD", + BD: "BDT", + }; + + expect(currentOutput).toEqual(targetOutput); +}); diff --git a/Sprint-2/implement/querystring.js b/Sprint-2/implement/querystring.js index 45ec4e5f3..e8f6b3297 100644 --- a/Sprint-2/implement/querystring.js +++ b/Sprint-2/implement/querystring.js @@ -4,10 +4,37 @@ function parseQueryString(queryString) { return queryParams; } const keyValuePairs = queryString.split("&"); + const emptyElement = ""; - for (const pair of keyValuePairs) { - const [key, value] = pair.split("="); - queryParams[key] = value; + for (i = 0; i < keyValuePairs.length; i++) { + const currentPair = keyValuePairs[i]; + if (currentPair !== emptyElement) { + const index = currentPair.indexOf("="); + if (index !== -1) { + const key = currentPair.slice(0, index); + const value = currentPair.slice(index + 1); + const decodeKey = decodeURIComponent(key); + const decodeValue = decodeURIComponent(value); + const replacedKey = decodeKey.replace("+", " "); + const replacedValue = decodeValue.replace("+", " "); + if (queryParams.hasOwnProperty(replacedKey)) { + if (Array.isArray(queryParams[replacedKey])) { + queryParams[replacedKey].push(replacedValue); + } else { + queryParams[replacedKey] = [ + queryParams[replacedKey], + replacedValue, + ]; + } + } else { + queryParams[replacedKey] = replacedValue; + } + } else if (index === -1) { + const key = currentPair; + const value = ""; + queryParams[key] = value; + } + } } return queryParams; diff --git a/Sprint-2/implement/querystring.test.js b/Sprint-2/implement/querystring.test.js index 328b8df61..82bc37dfc 100644 --- a/Sprint-2/implement/querystring.test.js +++ b/Sprint-2/implement/querystring.test.js @@ -9,7 +9,7 @@ test("should parse values containing '='", () => { expect(parseQueryString("equation=a=b-2")).toEqual({ equation: "a=b-2", }); -}); +}); test("should ignore empty key-value pairs", () => { expect(parseQueryString("key1=value1&&key2=value2&")).toEqual({ diff --git a/Sprint-2/implement/tally.js b/Sprint-2/implement/tally.js index f47321812..cf1fc7460 100644 --- a/Sprint-2/implement/tally.js +++ b/Sprint-2/implement/tally.js @@ -1,3 +1,21 @@ -function tally() {} +function tally(array) { + if (array.length === 0) { + return {}; + } + if (typeof array === "string") { + throw new Error(); + } + const object = {}; + for (i = 0; i < array.length; i++) { + const currentItem = array[i]; + if (object.hasOwnProperty(currentItem)) { + object[currentItem] += 1; + } else { + object[currentItem] = 1; + } + } + + return object; +} module.exports = tally; diff --git a/Sprint-2/implement/tally.test.js b/Sprint-2/implement/tally.test.js index 2ceffa8dd..fe5558d39 100644 --- a/Sprint-2/implement/tally.test.js +++ b/Sprint-2/implement/tally.test.js @@ -1,34 +1,18 @@ const tally = require("./tally.js"); -/** - * tally array - * - * In this task, you'll need to implement a function called tally - * that will take a list of items and count the frequency of each item - * in an array - * - * For example: - * - * tally(['a']), target output: { a: 1 } - * tally(['a', 'a', 'a']), target output: { a: 3 } - * tally(['a', 'a', 'b', 'c']), target output: { a : 2, b: 1, c: 1 } - */ +test("should return an empty object when given an empty array", () => { + expect(tally([])).toEqual({}); +}); -// Acceptance criteria: +test("should return counts for each unique item when given an array with one item and with duplicate items", () => { + expect(tally(["a"])).toEqual({ a: 1 }); + expect(tally(["a", "a", "a"])).toEqual({ a: 3 }); + expect(tally(["a", "a", "b", "c"])).toEqual({ a: 2, b: 1, c: 1 }); +}); -// Given a function called tally -// When passed an array of items -// Then it should return an object containing the count for each unique item - -// 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"); - -// Given an array with duplicate items -// When passed to tally -// Then it should return counts for each unique item - -// Given an invalid input like a string -// When passed to tally -// Then it should throw an error +test("should throw an error when given an invalid input", () => { + function invalidInput() { + tally("string"); + } + expect(invalidInput).toThrow(Error); +}); diff --git a/Sprint-2/interpret/invert.js b/Sprint-2/interpret/invert.js index bb353fb1f..b85480379 100644 --- a/Sprint-2/interpret/invert.js +++ b/Sprint-2/interpret/invert.js @@ -10,20 +10,33 @@ 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() returns an array of an object's own enumerable string-keyed property [key, value] pairs. +// It is needed in this program because the invert function needs access to both the original keys and values +// so it can swap them and create a new inverted object. // d) Explain why the current return value is different from the target output +// The current return value is different from the target output because invertedObj.key = value uses the word "key" as the property +// name instead of using the actual key variable. It stores the values under the same property name "key", +// so it does not swap the original key and value. The correct code should use invertedObj[value] = key to create value:key pairs. // 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 000000000..4cf99ed0d --- /dev/null +++ b/Sprint-2/interpret/invert.test.js @@ -0,0 +1,5 @@ +const invert = require("./invert.js"); + +test("should return swaped keys and values for an given object", () => { + expect(invert({ a: 1, b: 2 })).toEqual({ "1": "a", "2": "b"}); +}); \ No newline at end of file