From 917e5632378a9f7ce4da252a140df549d2d38ae1 Mon Sep 17 00:00:00 2001 From: dcostaprakash Date: Fri, 20 Mar 2026 14:00:03 +0000 Subject: [PATCH] Done the coursework exercises --- Sprint-2/debug/address.js | 4 ++- Sprint-2/debug/author.js | 4 ++- Sprint-2/debug/recipe.js | 6 +++-- Sprint-2/implement/contains.js | 7 +++++- Sprint-2/implement/contains.test.js | 27 +++++++++++++++++++- Sprint-2/implement/lookup.js | 12 +++++++-- Sprint-2/implement/lookup.test.js | 34 +++++++++++++++++++++++++- Sprint-2/implement/querystring.js | 4 ++- Sprint-2/implement/querystring.test.js | 16 ++++++++++-- Sprint-2/implement/tally.js | 18 +++++++++++++- Sprint-2/implement/tally.test.js | 20 ++++++++++++++- Sprint-2/interpret/invert.js | 11 ++++++++- 12 files changed, 148 insertions(+), 15 deletions(-) diff --git a/Sprint-2/debug/address.js b/Sprint-2/debug/address.js index 940a6af83..ee29c766e 100644 --- a/Sprint-2/debug/address.js +++ b/Sprint-2/debug/address.js @@ -1,5 +1,7 @@ // Predict and explain first... +// This code will fail since we have put Index 0 instead of the key houseNumber + // This code should log out the houseNumber from the address object // but it isn't working... // Fix anything that isn't working @@ -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..1f76d1e36 100644 --- a/Sprint-2/debug/author.js +++ b/Sprint-2/debug/author.js @@ -1,5 +1,7 @@ // Predict and explain first... +//for...of cannot iterate plain objects directly, so it will display an error + // 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 +13,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..faa2b5f30 100644 --- a/Sprint-2/debug/recipe.js +++ b/Sprint-2/debug/recipe.js @@ -1,5 +1,7 @@ // Predict and explain first... +// It will print the whole object instead of the ingredients list. To get it one below the other, we need to join the array and then \n to get it on separate lines + // 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? @@ -11,5 +13,5 @@ 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..4db2b06a5 100644 --- a/Sprint-2/implement/contains.js +++ b/Sprint-2/implement/contains.js @@ -1,3 +1,8 @@ -function contains() {} +function contains(object, propertyName) { + if (typeof object !== "object" || object === null || Array.isArray(object)) { + return false; + } + return Object.prototype.hasOwnProperty.call(object, propertyName); +} module.exports = contains; diff --git a/Sprint-2/implement/contains.test.js b/Sprint-2/implement/contains.test.js index 326bdb1f2..44d8aacc0 100644 --- a/Sprint-2/implement/contains.test.js +++ b/Sprint-2/implement/contains.test.js @@ -20,16 +20,41 @@ 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("returns false when object is empty", () => { + expect(contains({}, "anyProp")).toBe(false); +}); // Given an object with properties // When passed to contains with an existing property name // Then it should return true +test("returns true for object with existing property name", () => { + expect(contains({ name: "Brad" }, "name")).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 for object with non-existent property name", () => { + expect(contains({ name: "Brad" }, "age")).toBe(false); +}); // Given invalid parameters like an array // When passed to contains // Then it should return false or throw an error +test("returns false when input is an array", () => { + expect(contains(["Brad"], "0")).toBe(false); +}); + +// Given null inputs +// When passed to contains +// Then it should return false +test("returns false when input is null", () => { + expect(contains(null, "name")).toBe(false); +}); + +// Given non object inputs like string or number +// When passed to contains +// Then it should return false +test("returns false when input is a string", () => { + expect(contains("Brad", "name")).toBe(false); +}); diff --git a/Sprint-2/implement/lookup.js b/Sprint-2/implement/lookup.js index a6746e07f..985523d18 100644 --- a/Sprint-2/implement/lookup.js +++ b/Sprint-2/implement/lookup.js @@ -1,5 +1,13 @@ -function createLookup() { - // implementation here +function createLookup(countryCurrencyPairs) { + const lookup = {}; + + for (const pair of countryCurrencyPairs) { + const countryCode = pair[0]; + const currencyCode = pair[1]; + 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..7249f3904 100644 --- a/Sprint-2/implement/lookup.test.js +++ b/Sprint-2/implement/lookup.test.js @@ -1,7 +1,39 @@ 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 input = [ + ["US", "USD"], + ["CA", "CAD"], + ]; + const result = createLookup(input); + expect(result).toEqual({ + US: "USD", + CA: "CAD", + }); +}); +test("creates a lookup for 1 pair", () => { + const input = [["IN", "INR"]]; + const result = createLookup(input); + expect(result).toEqual({ + IN: "INR", + }); +}); + +test("returns an empty object when input array is empty", () => { + const input = []; + const result = createLookup(input); + expect(result).toEqual({}); +}); + +test("ignores cases where it is invalid within other pairs", () => { + const input = [["US", "USD"], ["Invalid"], ["CA", "CAD"]]; + const result = createLookup(input); + expect(result).toEqual({ + US: "USD", + CA: "CAD", + }); +}); /* Create a lookup object of key value pairs from an array of code pairs diff --git a/Sprint-2/implement/querystring.js b/Sprint-2/implement/querystring.js index 45ec4e5f3..84b6a41fa 100644 --- a/Sprint-2/implement/querystring.js +++ b/Sprint-2/implement/querystring.js @@ -6,7 +6,9 @@ function parseQueryString(queryString) { const keyValuePairs = queryString.split("&"); for (const pair of keyValuePairs) { - const [key, value] = pair.split("="); + const index = pair.indexOf("="); + const key = pair.slice(0, index); + const value = pair.slice(index + 1); queryParams[key] = value; } diff --git a/Sprint-2/implement/querystring.test.js b/Sprint-2/implement/querystring.test.js index 3e218b789..7c4a1a487 100644 --- a/Sprint-2/implement/querystring.test.js +++ b/Sprint-2/implement/querystring.test.js @@ -3,10 +3,22 @@ // 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", + }); +}); + +test("parses with missing value", () => { + expect(parseQueryString("a=")).toEqual({ + a: "", + }); +}); + +test("parses with missing key", () => { + expect(parseQueryString("=value")).toEqual({ + "": "value", }); }); diff --git a/Sprint-2/implement/tally.js b/Sprint-2/implement/tally.js index f47321812..1545b5e52 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 result = {}; + + for (const item of items) { + if (result[item]) { + result[item]++; + } else { + result[item] = 1; + } + } + + return result; +} module.exports = tally; diff --git a/Sprint-2/implement/tally.test.js b/Sprint-2/implement/tally.test.js index 2ceffa8dd..fdc1d5ffd 100644 --- a/Sprint-2/implement/tally.test.js +++ b/Sprint-2/implement/tally.test.js @@ -23,12 +23,30 @@ 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({}); +}); // Given an array with duplicate items // When passed to tally // Then it should return counts for each unique item +test("tally returns count for each unique item", () => { + const input = ["a", "a", "b", "b", "c", "a"]; + expect(tally(input)).toEqual({ a: 3, b: 2, c: 1 }); +}); // Given an invalid input like a string // When passed to tally // Then it should throw an error +test("tally throws an error for invalid input", () => { + expect(() => { + tally("abc"); + }).toThrow(); +}); + +// Given a single item +// When passed to tally +// Then it should return count for the item +test("tally returns count for single item", () => { + expect(tally(["d"])).toEqual({ d: 1 }); +}); diff --git a/Sprint-2/interpret/invert.js b/Sprint-2/interpret/invert.js index bb353fb1f..de15ff4fe 100644 --- a/Sprint-2/interpret/invert.js +++ b/Sprint-2/interpret/invert.js @@ -10,20 +10,29 @@ 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 } +// The current value before fix is {key: 1} // b) What is the current return value when invert is called with { a: 1, b: 2 } +// The current value before fix is {key: 2} since it overrides the previous value // c) What is the target return value when invert is called with {a : 1, b: 2} +// The target return value should have been {"1": "a","2": "b"} // c) What does Object.entries return? Why is it needed in this program? +// Object.entries returns the array of [key, value] pairs +// For eg: Object.entries({a: 5, b: 7}) +// returns: [["a", 5], ["b", 7]] +// It is needed since it allows to Loop through both key and value at the same time. // d) Explain why the current return value is different from the target output +// The current return value is different from the target output since "key" is treated as a literal property name. We need to use variable value as the key // e) Fix the implementation of invert (and write tests to prove it's fixed!) +// Done