diff --git a/Sprint-2/debug/address.js b/Sprint-2/debug/address.js index 940a6af83..3bcdc4ed5 100644 --- a/Sprint-2/debug/address.js +++ b/Sprint-2/debug/address.js @@ -5,11 +5,11 @@ // Fix anything that isn't working const address = { - houseNumber: 42, + houseNumber: "42", street: "Imaginary Road", city: "Manchester", country: "England", 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..2ead84603 100644 --- a/Sprint-2/debug/author.js +++ b/Sprint-2/debug/author.js @@ -2,7 +2,7 @@ // 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 - +// this function have 2 bugs, one is that the since object is not iterable so by using for of loop is will not work. The second is that is only print out property name not property value const author = { firstName: "Zadie", lastName: "Smith", @@ -11,6 +11,6 @@ const author = { alive: true, }; -for (const value of author) { - console.log(value); +for (const value in author) { + console.log(author[value]); } diff --git a/Sprint-2/debug/recipe.js b/Sprint-2/debug/recipe.js index 6cbdd22cd..a413b3332 100644 --- a/Sprint-2/debug/recipe.js +++ b/Sprint-2/debug/recipe.js @@ -12,4 +12,4 @@ const recipe = { console.log(`${recipe.title} serves ${recipe.serves} ingredients: -${recipe}`); +${recipe.ingredients.join("\n")}`); diff --git a/Sprint-2/implement/contains.js b/Sprint-2/implement/contains.js index cd779308a..850d98933 100644 --- a/Sprint-2/implement/contains.js +++ b/Sprint-2/implement/contains.js @@ -1,3 +1,18 @@ -function contains() {} +function contains(object, key) { + if (object === null || typeof object !== "object" || Array.isArray(object)) { + return false; + } + + if (Object.keys(object).length === 0) { + return false; + } + + for (const item in object) { + if (item === key) { + return true; + } + } + return false; +} module.exports = contains; diff --git a/Sprint-2/implement/contains.test.js b/Sprint-2/implement/contains.test.js index 326bdb1f2..6b0573531 100644 --- a/Sprint-2/implement/contains.test.js +++ b/Sprint-2/implement/contains.test.js @@ -16,20 +16,46 @@ 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 +test("should return true if the input object contain the key value", () => { + expect(contains({ a: 1, b: 2, c: 3, d: 4 }, "a")).toBe(true); + expect(contains({ a: 1, b: 2, c: 3, d: 4 }, "f")).toBe(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 the object is empty", () => { + let emptyObject = {}; + expect(contains(emptyObject, "a")).toBe(false); +}); // Given an object with properties // When passed to contains with an existing property name // Then it should return true +test("should return true if the input object contain existing key value", () => { + let infoObject = { name: "John", age: "24", city: "Glasgow" }; + expect(contains(infoObject, "name")).toBe(true); + expect(contains(infoObject, "age")).toBe(true); + expect(contains(infoObject, "city")).toBe(true); +}); // Given an object with properties // When passed to contains with a non-existent property name // Then it should return false - +test("should return false if the input object do not have non existing key value", () => { + let goodsObject = { fruit: "Apple", amount: "50", origin: "Glasgow" }; + expect(contains(goodsObject, "tree")).toBe(false); + expect(contains(goodsObject, "color")).toBe(false); + expect(contains(goodsObject, "country")).toBe(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 the input parameters is an array or not an object", () => { + let mixObject = { a: 2, b: "hello", c: [] }; + expect(contains(mixObject, "c")).toBe(true); + expect(contains(["A", "B"], "1")).toBe(false); + expect(contains(null, "1")).toBe(false); + expect(contains(undefined, "1")).toBe(false); + expect(contains("ABC", "1")).toBe(false); +}); diff --git a/Sprint-2/implement/lookup.js b/Sprint-2/implement/lookup.js index a6746e07f..39330cd17 100644 --- a/Sprint-2/implement/lookup.js +++ b/Sprint-2/implement/lookup.js @@ -1,5 +1,11 @@ -function createLookup() { +function createLookup(pairs) { // implementation here + let countryPair = {}; + for (const [country, currency] of pairs) { + countryPair[country] = currency; + } + + return countryPair; } module.exports = createLookup; diff --git a/Sprint-2/implement/lookup.test.js b/Sprint-2/implement/lookup.test.js index 547e06c5a..d77330cc2 100644 --- a/Sprint-2/implement/lookup.test.js +++ b/Sprint-2/implement/lookup.test.js @@ -1,6 +1,23 @@ const createLookup = require("./lookup.js"); -test.todo("creates a country currency code lookup for multiple codes"); +test("creates a country currency code in an lookup array for multiple codes, and return in object form", () => { + let worldCurrency = [ + ["US", "USD"], + ["GB", "POUND"], + ["FRC", "EURO"], + ["JP", "YEN"], + ["KR", "WON"], + ["MX", "PESO"], + ]; + expect(createLookup(worldCurrency)).toEqual({ + US: "USD", + GB: "POUND", + FRC: "EURO", + JP: "YEN", + KR: "WON", + MX: "PESO", + }); +}); /* diff --git a/Sprint-2/implement/querystring.js b/Sprint-2/implement/querystring.js index 45ec4e5f3..5ccd31d75 100644 --- a/Sprint-2/implement/querystring.js +++ b/Sprint-2/implement/querystring.js @@ -1,12 +1,20 @@ function parseQueryString(queryString) { const queryParams = {}; - if (queryString.length === 0) { + + if (!queryString || queryString.length === 0) { return queryParams; } - const keyValuePairs = queryString.split("&"); - for (const pair of keyValuePairs) { - const [key, value] = pair.split("="); + const pairs = queryString.split("&"); + + for (const pair of pairs) { + if (!pair) continue; + + const [key, ...rest] = pair.split("="); + const value = rest.join("="); + + if (!key) continue; + queryParams[key] = value; } diff --git a/Sprint-2/implement/querystring.test.js b/Sprint-2/implement/querystring.test.js index 3e218b789..717ee6901 100644 --- a/Sprint-2/implement/querystring.test.js +++ b/Sprint-2/implement/querystring.test.js @@ -3,10 +3,10 @@ // Below is one test case for an edge case the implementation doesn't handle well. // Fix the implementation for this test, and try to think of as many other edge cases as possible - write tests and fix those too. -const parseQueryString = require("./querystring.js") +const parseQueryString = require("./querystring.js"); test("parses querystring values containing =", () => { expect(parseQueryString("equation=x=y+1")).toEqual({ - "equation": "x=y+1", + equation: "x=y+1", }); }); diff --git a/Sprint-2/implement/tally.js b/Sprint-2/implement/tally.js index f47321812..1cff3b5c2 100644 --- a/Sprint-2/implement/tally.js +++ b/Sprint-2/implement/tally.js @@ -1,3 +1,18 @@ -function tally() {} +function tally(sumArray) { + if (!Array.isArray(sumArray)) { + throw new Error("Input musk be an array"); + } + + if (sumArray.length === 0) return {}; + let totalSum = {}; + for (const item of sumArray) { + if (!Object.hasOwn(totalSum, item)) { + totalSum[item] = 1; + } else { + totalSum[item] += 1; + } + } + return totalSum; +} module.exports = tally; diff --git a/Sprint-2/implement/tally.test.js b/Sprint-2/implement/tally.test.js index 2ceffa8dd..a1f891333 100644 --- a/Sprint-2/implement/tally.test.js +++ b/Sprint-2/implement/tally.test.js @@ -23,12 +23,22 @@ 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("tally on an empty array returns an empty object", () => { + let emptyObject = []; + expect(tally(emptyObject)).toEqual({}); +}); // Given an array with duplicate items // When passed to tally // Then it should return counts for each unique item - +test("tally on an array with duplicate returns with correct amount object", () => { + let firstObject = ["a", "a", "c", "b", "c", "b", "a", "c"]; + expect(tally(firstObject)).toEqual({ a: 3, b: 2, c: 3 }); +}); // Given an invalid input like a string // When passed to tally // Then it should throw an error +test("tally on a string should throw invalid input", () => { + let stringInput = "input"; + expect(() => tally(stringInput)).toThrow("Input musk be an array"); +}); diff --git a/Sprint-2/interpret/invert.js b/Sprint-2/interpret/invert.js index bb353fb1f..c89a3aee5 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; } +console.log(invert({ a: 1 })); +console.log(invert({ a: 1, b: 2 })); +console.log(invert({ a: 1, b: 2 })); // a) What is the current return value when invert is called with { a : 1 } +// the current output value of this invert is {key:1}. // b) What is the current return value when invert is called with { a: 1, b: 2 } +/* the current output value of this invert is {key:2} because +instead of accessing the [key] variable inside the condition parameter for store key for each loop, the code in line 13 instead adding .key behind and the system +interpret as string literal so each loop is overwrite the previous key value. +.*/ // c) What is the target return value when invert is called with {a : 1, b: 2} +// is also the same with { a: 1, b: 2 } because is overwrite the previous key each loop. // c) What does Object.entries return? Why is it needed in this program? +/* Object.entries return deconstruct object key and value pair into individual item in an array. +This is very important because since object is are not iterable, this help by deconstructing object into array when we try to loop each key and value pair*/ // d) Explain why the current return value is different from the target output - +/* +Because inside the for of loop, the invertedObj output assign they keys first instead of value for each loop. +*/ // e) Fix the implementation of invert (and write tests to prove it's fixed!)