diff --git a/Sprint-2/debug/address.js b/Sprint-2/debug/address.js index 940a6af83..4478297f8 100644 --- a/Sprint-2/debug/address.js +++ b/Sprint-2/debug/address.js @@ -1,5 +1,6 @@ // Predict and explain first... - +//Answer: The console.log will print "My house number is undefined"" because we are trying to access the house number with the index of an array, +// but our address in as object with key and value. We can fix this by replacing console.log(`My house number is ${address[0]}`); with console.log(`My house number is ${address.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 +13,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..e8d33b68e 100644 --- a/Sprint-2/debug/author.js +++ b/Sprint-2/debug/author.js @@ -1,5 +1,7 @@ // Predict and explain first... - +// Answer" In this code there is a problem on how the for..loop was used. For... loop can only be used with an iterable objects such as string,arrays etc but in this code author is just an ordinary object +// and in console.log we aretrying to log value inside the loop, but there's no value being defined as part of for...of. The loop is designed to loop over iterable elements, not object properties. +// To fix this we can use for...in to access properties directly. // 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) { - console.log(value); +for (const property in author) { + console.log(author[property]); } diff --git a/Sprint-2/debug/recipe.js b/Sprint-2/debug/recipe.js index 6cbdd22cd..f8f2da5f9 100644 --- a/Sprint-2/debug/recipe.js +++ b/Sprint-2/debug/recipe.js @@ -1,5 +1,6 @@ // Predict and explain first... - +//The problem here is that the code is logging the entire recipe object instead of the individual ingredients. +//To fix this we have to used for..of loop to list each ingredient on a new line // 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,8 @@ const recipe = { ingredients: ["olive oil", "tomatoes", "salt", "pepper"], }; -console.log(`${recipe.title} serves ${recipe.serves} - ingredients: -${recipe}`); +console.log(`${recipe.title} serves ${recipe.serves}`); +console.log("ingredients:"); +for (const ingredient of recipe.ingredients) { + console.log(ingredient); +} diff --git a/Sprint-2/implement/contains.js b/Sprint-2/implement/contains.js index cd779308a..a10393f9f 100644 --- a/Sprint-2/implement/contains.js +++ b/Sprint-2/implement/contains.js @@ -1,3 +1,8 @@ -function contains() {} - +function contains(object, key) { + if (typeof object === "object" && object !== null && !Array.isArray(object)) { + return key in object; + } else { + return false; + } +} module.exports = contains; diff --git a/Sprint-2/implement/contains.test.js b/Sprint-2/implement/contains.test.js index 326bdb1f2..104feed5d 100644 --- a/Sprint-2/implement/contains.test.js +++ b/Sprint-2/implement/contains.test.js @@ -20,16 +20,46 @@ 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", () => { + const input = {}; + const key = "newKey"; + const currentOutput = contains(input, key); + const targetOutput = false; + + expect(currentOutput).toEqual(targetOutput); +}); // Given an object with properties // When passed to contains with an existing property name // Then it should return true +test("contains on object with properties returns true", () => { + const input = { x: 1, y: 2 }; + const key = "x"; + const currentOutput = contains(input, key); + const targetOutput = true; + + expect(currentOutput).toEqual(targetOutput); +}); // Given an object with properties // When passed to contains with a non-existent property name // Then it should return false +test("contains on object with non -existent properties returns false", () => { + const input = { x: 1, y: 2 }; + const key = "z"; + const currentOutput = contains(input, key); + const targetOutput = false; + expect(currentOutput).toEqual(targetOutput); +}); // Given invalid parameters like an array // When passed to contains // Then it should return false or throw an error +test("contains on object with invalid parameters returns false", () => { + const input = [123, "yellowfruit", null, undefined]; + const key = "color"; + const currentOutput = contains(input, key); + const targetOutput = false; + + expect(currentOutput).toEqual(targetOutput); +}); diff --git a/Sprint-2/implement/lookup.js b/Sprint-2/implement/lookup.js index a6746e07f..6087861c1 100644 --- a/Sprint-2/implement/lookup.js +++ b/Sprint-2/implement/lookup.js @@ -1,5 +1,11 @@ -function createLookup() { - // implementation here +function createLookup(pairs) { + const lookUp = {}; + for (pair of pairs) { + const key = pair[0]; + const value = pair[1]; + lookUp[key] = value; + } + return lookUp; } module.exports = createLookup; diff --git a/Sprint-2/implement/lookup.test.js b/Sprint-2/implement/lookup.test.js index 547e06c5a..b6d25deb0 100644 --- a/Sprint-2/implement/lookup.test.js +++ b/Sprint-2/implement/lookup.test.js @@ -1,6 +1,18 @@ 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 currentOutput = createLookup(input); + const targetOutput = { + US: "USD", + CA: "CAD", + }; + + expect(currentOutput).toEqual(targetOutput); +}); /* @@ -18,7 +30,7 @@ When Then - It should return an object where: - The keys are the country codes - - The values are the corresponding currency codes + - The values are the correspondiSng currency codes Example Given: [['US', 'USD'], ['CA', 'CAD']] diff --git a/Sprint-2/implement/querystring.js b/Sprint-2/implement/querystring.js index 45ec4e5f3..eb877c539 100644 --- a/Sprint-2/implement/querystring.js +++ b/Sprint-2/implement/querystring.js @@ -1,12 +1,21 @@ function parseQueryString(queryString) { const queryParams = {}; + + // If the query string is empty, return an empty object if (queryString.length === 0) { return queryParams; } + const keyValuePairs = queryString.split("&"); for (const pair of keyValuePairs) { - const [key, value] = pair.split("="); + // Split by the first "=" into key and the rest into value + const [key, ...valueParts] = pair.split("="); + + // Join any remaining parts of the value after the first "=" + const value = valueParts.join("="); + + // Assign key-value pair to the result object queryParams[key] = value; } diff --git a/Sprint-2/implement/querystring.test.js b/Sprint-2/implement/querystring.test.js index 3e218b789..cecc686d2 100644 --- a/Sprint-2/implement/querystring.test.js +++ b/Sprint-2/implement/querystring.test.js @@ -3,10 +3,38 @@ // 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("returns an empty object for an empty query string", () => { + const input = ""; + const output = parseQueryString(input); + const expectedOutput = {}; + expect(output).toEqual(expectedOutput); +}); + +test("parses querystring with one key-value pair", () => { + const input = "name=John"; + const output = parseQueryString(input); + const expectedOutput = { name: "John" }; + expect(output).toEqual(expectedOutput); +}); + +test("parses querystring with multiple key-value pairs", () => { + const input = "name=John&age=30&city=New York"; + const output = parseQueryString(input); + const expectedOutput = { name: "John", age: "30", city: "New York" }; + expect(output).toEqual(expectedOutput); +}); + +test("parses querystring with empty values", () => { + const input = "key1=&key2="; + const output = parseQueryString(input); + const expectedOutput = { key1: "", key2: "" }; + expect(output).toEqual(expectedOutput); +}); diff --git a/Sprint-2/implement/tally.js b/Sprint-2/implement/tally.js index f47321812..459a301c7 100644 --- a/Sprint-2/implement/tally.js +++ b/Sprint-2/implement/tally.js @@ -1,3 +1,15 @@ -function 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; diff --git a/Sprint-2/implement/tally.test.js b/Sprint-2/implement/tally.test.js index 2ceffa8dd..8bf62dcf6 100644 --- a/Sprint-2/implement/tally.test.js +++ b/Sprint-2/implement/tally.test.js @@ -20,15 +20,42 @@ const tally = require("./tally.js"); // When passed an array of items // Then it should return an object containing the count for each unique item +test("tally returns an object with counts for each unique item", () => { + const input = ["a", "a", "b", "c"]; + const currentOutput = tally(input); + const targetOutput = { a: 2, b: 1, c: 1 }; + expect(currentOutput).toEqual(targetOutput); +}); + // 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", () => { + const input = []; + const currentOutput = tally(input); + const targetOutput = {}; + + expect(currentOutput).toEqual(targetOutput); +}); // 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 itemsreturns an object containing counts for each unique item", () => { + const input = ["a", "a", "a"]; + const currentOutput = tally(input); + const targetOutput = { a: 3 }; + + expect(currentOutput).toEqual(targetOutput); +}); // Given an invalid input like a string // When passed to tally // Then it should throw an error +test("tally throws an error on invalid inputs (null, string, undefined)", () => { + const invalidInputs = [null, "a,a,a", undefined]; + + invalidInputs.forEach((input) => { + expect(() => tally(input)).toThrow("Input must be an array"); + }); +}); diff --git a/Sprint-2/interpret/invert.js b/Sprint-2/interpret/invert.js index bb353fb1f..1d11542b8 100644 --- a/Sprint-2/interpret/invert.js +++ b/Sprint-2/interpret/invert.js @@ -10,20 +10,34 @@ 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({ x: 10, y: 20 })); +console.log(invert({ x: 11, y: 11 })); + // a) What is the current return value when invert is called with { a : 1 } +// The return value is{ key: 1 } // b) What is the current return value when invert is called with { a: 1, b: 2 } +// The return value is { key: 2 } // c) What is the target return value when invert is called with {a : 1, b: 2} +// The target return value is { "1": "a", "2": "b" } // c) What does Object.entries return? Why is it needed in this program? +// It returns an array of key-value pairs (arrays), where each pair consists of a key and its corresponding value from the object. +// his is needed in the program because it allows you to iterate over each key-value pair in the object, so you can swap the keys and values. // d) Explain why the current return value is different from the target output +// We are always assigning the value to the property "key", rather than assigning the property name to the key from the original object. +//In the loop, we overwrite the key each time, which leads to only one property (the last one in the loop) being saved in the 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 000000000..3ee115f46 --- /dev/null +++ b/Sprint-2/interpret/invert.test.js @@ -0,0 +1,38 @@ +const invert = require("./invert"); + +describe("invert function", () => { + // Test case 1: Invert a single key-value pair + test("should invert a single key-value pair", () => { + const input = { a: 1 }; + const expected = { 1: "a" }; + expect(invert(input)).toEqual(expected); + }); + + // Test case 2: Invert multiple key-value pairs + test("should invert multiple key-value pairs", () => { + const input = { a: 1, b: 2 }; + const expected = { 1: "a", 2: "b" }; + expect(invert(input)).toEqual(expected); + }); + + // Test case 3: Invert object with different types of values + test("should invert an object with mixed types", () => { + const input = { x: 10, y: 20 }; + const expected = { 10: "x", 20: "y" }; + expect(invert(input)).toEqual(expected); + }); + + // Test case 4: Invert an empty object + test("should return an empty object when input is empty", () => { + const input = {}; + const expected = {}; + expect(invert(input)).toEqual(expected); // An empty object should return an empty object + }); + + // Test case 5: Invert object with the same values + test("should invert and handle duplicate values", () => { + const input = { a: 1, b: 1 }; + const expected = { 1: "b" }; + expect(invert(input)).toEqual(expected); + }); +});