diff --git a/Sprint-2/debug/address.js b/Sprint-2/debug/address.js index 940a6af83..769b960b4 100644 --- a/Sprint-2/debug/address.js +++ b/Sprint-2/debug/address.js @@ -1,4 +1,7 @@ -// Predict and explain first... +// Prediction: +// The code will print "My house number is undefined" +// because address[0] does not exist. +// Objects use keys like address.houseNumber, not numbers. // 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]}`); +// Use the correct key of the object +console.log(`My house number is ${address.houseNumber}`); diff --git a/Sprint-2/debug/author.js b/Sprint-2/debug/author.js index 8c2125977..beffa693b 100644 --- a/Sprint-2/debug/author.js +++ b/Sprint-2/debug/author.js @@ -1,8 +1,9 @@ // Predict and explain first... - +//The code will give an error because for...of does not work on objects. // 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 is because for...of only works with arrays or strings. + An object like author is not an array, so the code will give an error. Nothing will be printed.*/ const author = { firstName: "Zadie", lastName: "Smith", @@ -11,6 +12,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 6cbdd22cd..771e5608e 100644 --- a/Sprint-2/debug/recipe.js +++ b/Sprint-2/debug/recipe.js @@ -1,4 +1,5 @@ // Predict and explain first... +/*The code will show [object Object] instead of the ingredients because it tries to print the whole object as a string.*/ // 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 +12,6 @@ const recipe = { }; console.log(`${recipe.title} serves ${recipe.serves} - ingredients: -${recipe}`); +ingredients: +${recipe.ingredients.join("\n")}`); + diff --git a/Sprint-2/implement/contains.js b/Sprint-2/implement/contains.js index cd779308a..4dee84379 100644 --- a/Sprint-2/implement/contains.js +++ b/Sprint-2/implement/contains.js @@ -1,3 +1,9 @@ -function contains() {} +function contains(obj, key) { + if (typeof obj !== "object" || obj === null || Array.isArray(obj)) { + return false; + } + + return Object.prototype.hasOwnProperty.call(obj, key); +} module.exports = contains; diff --git a/Sprint-2/implement/contains.test.js b/Sprint-2/implement/contains.test.js index 326bdb1f2..09c826e89 100644 --- a/Sprint-2/implement/contains.test.js +++ b/Sprint-2/implement/contains.test.js @@ -20,16 +20,33 @@ 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("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("contains returns true for existing property", () => { + const obj = { a: 1, b: 2 }; + expect(contains(obj, "a")).toBe(true); +}); + // Given an object with properties // When passed to contains with a non-existent property name // Then it should return false +test("contains returns false for non-existent property", () => { + const obj = { a: 1, b: 2 }; + expect(contains(obj, "c")).toBe(false); +}); + // Given invalid parameters like an array // When passed to contains // Then it should return false or throw an error +test("contains returns false for invalid input", () => { + expect(contains([], "length")).toBe(false); + expect(contains(null, "a")).toBe(false); + expect(contains("string", "a")).toBe(false); +}); diff --git a/Sprint-2/implement/lookup.js b/Sprint-2/implement/lookup.js index a6746e07f..48c25a3e2 100644 --- a/Sprint-2/implement/lookup.js +++ b/Sprint-2/implement/lookup.js @@ -1,5 +1,14 @@ -function createLookup() { - // implementation here +function createLookup(pairs) { + const lookup = {}; + + for (let i = 0; i < pairs.length; i++) { + const pair = pairs[i]; + const country = pair[0]; + const currency = pair[1]; + 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..62975fbcc 100644 --- a/Sprint-2/implement/lookup.test.js +++ b/Sprint-2/implement/lookup.test.js @@ -33,3 +33,14 @@ It should return: 'CA': 'CAD' } */ + +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..55b7de1fe 100644 --- a/Sprint-2/implement/querystring.js +++ b/Sprint-2/implement/querystring.js @@ -1,12 +1,25 @@ function parseQueryString(queryString) { const queryParams = {}; - if (queryString.length === 0) { + if (!queryString) { return queryParams; } + + function decodePart(text) { + try { + return decodeURIComponent(text); + } catch (error) { + return text; + } + } + const keyValuePairs = queryString.split("&"); for (const pair of keyValuePairs) { - const [key, value] = pair.split("="); + const index = pair.indexOf("="); // find the first = + if (index === -1) continue; // skip if no = + + const key = decodePart(pair.substring(0, index)); // before the = + const value = decodePart(pair.substring(index + 1)); // after the = queryParams[key] = value; } diff --git a/Sprint-2/implement/querystring.test.js b/Sprint-2/implement/querystring.test.js index 3e218b789..3193b5284 100644 --- a/Sprint-2/implement/querystring.test.js +++ b/Sprint-2/implement/querystring.test.js @@ -10,3 +10,25 @@ test("parses querystring values containing =", () => { "equation": "x=y+1", }); }); + +test("parses empty query string", () => { + expect(parseQueryString("")).toEqual({}); +}); + +test("parses multiple key-value pairs", () => { + expect(parseQueryString("a=1&b=2")).toEqual({ a: "1", b: "2" }); +}); + +test("parses key with empty value", () => { + expect(parseQueryString("foo=")).toEqual({ foo: "" }); +}); + +test("parses empty key with value", () => { + expect(parseQueryString("=bar")).toEqual({ "": "bar" }); +}); + +test("parses url encoded key and value", () => { + expect(parseQueryString("tags%5B%5D=hello%20world")).toEqual({ + "tags[]": "hello world", + }); +}); diff --git a/Sprint-2/implement/tally.js b/Sprint-2/implement/tally.js index f47321812..2f0a61d15 100644 --- a/Sprint-2/implement/tally.js +++ b/Sprint-2/implement/tally.js @@ -1,3 +1,22 @@ -function tally() {} +function tally(arr) { + if (!Array.isArray(arr)) { + throw new Error("Input must be an array"); + } + + const counts = Object.create(null); + + for (let i = 0; i < arr.length; i++) { + const item = arr[i]; + + + if (Object.prototype.hasOwnProperty.call(counts, item)) { + counts[item] += 1; + } 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..cbe4a8198 100644 --- a/Sprint-2/implement/tally.test.js +++ b/Sprint-2/implement/tally.test.js @@ -23,12 +23,27 @@ 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", () => { + expect(tally([])).toEqual(Object.create(null)); +}); // Given an array with duplicate items // When passed to tally // Then it should return counts for each unique item +test("tally on an empty array returns an empty object", () => { + expect(tally([])).toEqual(Object.create(null)); +}); + +test("tally counts keys like toString", () => { + const expected = Object.create(null); + expected.toString = 2; + expect(tally(["toString", "toString"])).toEqual(expected); +}); + // Given an invalid input like a string // When passed to tally // Then it should throw an error +test("tally throws error for invalid input", () => { + expect(() => tally("not an array")).toThrow("Input must be an array"); +}); diff --git a/Sprint-2/interpret/invert.js b/Sprint-2/interpret/invert.js index bb353fb1f..ac4de901c 100644 --- a/Sprint-2/interpret/invert.js +++ b/Sprint-2/interpret/invert.js @@ -6,24 +6,64 @@ // E.g. invert({x : 10, y : 20}), target output: {"10": "x", "20": "y"} -function invert(obj) { +/*function invert(obj) { const invertedObj = {}; for (const [key, value] of Object.entries(obj)) { invertedObj.key = value; } + return invertedObj; +}*/ + +//fixed code: +function invert(obj) { + const invertedObj = {}; + + for (const [key, value] of Object.entries(obj)) { + invertedObj[value] = key; // use the value as key, key as value + } + return invertedObj; } +module.exports = invert; + // 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(obj) returns an array of [key, value] pairs. +Needed because we want to loop through keys and values at the same time.*/ // d) Explain why the current return value is different from the target output +/*Because the code is wrong in one place: + +invertedObj.key = value; + + +This uses the word "key" as a property name instead of using the variable key. + +So instead of creating: + +{ 1: "a" } + + +it always creates: + +{ key: 1 } + + +and for multiple items, it keeps overwriting the same "key" */ + + // e) Fix the implementation of invert (and write tests to prove it's fixed!) + // I did write upt there. \ No newline at end of file