diff --git a/Sprint-2/debug/address.js b/Sprint-2/debug/address.js index 940a6af83..9c79c04d9 100644 --- a/Sprint-2/debug/address.js +++ b/Sprint-2/debug/address.js @@ -1,4 +1,5 @@ // Predict and explain first... +// Answer: when the console.log runs, it will return `My house number is undefined` because `0` is not a key in the object so its value will be undefined // This code should log out the houseNumber from the address object // but it 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..f46621c84 100644 --- a/Sprint-2/debug/author.js +++ b/Sprint-2/debug/author.js @@ -1,4 +1,6 @@ // Predict and explain first... +// Answer: When the code runs, the console.log will output an error in the terminal because you can not use for...of loop for an object, +// instead, you use for...in loop // 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 value in author) { + console.log(author[value]); } diff --git a/Sprint-2/debug/recipe.js b/Sprint-2/debug/recipe.js index 6cbdd22cd..5020d1602 100644 --- a/Sprint-2/debug/recipe.js +++ b/Sprint-2/debug/recipe.js @@ -1,4 +1,5 @@ // Predict and explain first... +// Answer: When the code runs, `${recipe}` part of the console.log expression will output the string `[object Object]` // This program should log out the title, how many it serves and the ingredients. // Each ingredient should be logged on a new line @@ -12,4 +13,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..6431d8884 100644 --- a/Sprint-2/implement/contains.js +++ b/Sprint-2/implement/contains.js @@ -1,3 +1,9 @@ -function contains() {} +function contains(obj, propertyName) { + if (typeof obj !== "object" || obj === null || Array.isArray(obj)) { + throw new TypeError("Invalid input: obj must be a plain object"); + } + + return obj.hasOwnProperty(propertyName); +} module.exports = contains; diff --git a/Sprint-2/implement/contains.test.js b/Sprint-2/implement/contains.test.js index 326bdb1f2..08086d2fa 100644 --- a/Sprint-2/implement/contains.test.js +++ b/Sprint-2/implement/contains.test.js @@ -16,20 +16,53 @@ 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("Given an input of an object with a property name, returns true if the object contains the property", () => { + const objInput = { a: 1 }; + expect(contains(objInput, "a")).toBe(true); + expect(contains(objInput, "t")).toBe(false); +}); // Given an empty object // When passed to contains // Then it should return false -test.todo("contains on empty object returns false"); +test("Given an empty object, when passed to contains, returns false", () => { + const objInput = {}; + expect(contains(objInput, "a")).toBe(false); +}); // Given an object with properties // When passed to contains with an existing property name // Then it should return true +test("Given an object with properties, when passed to contains with an existing property name, returns true", () => { + const objInput = { name: "Alice", age: 25 }; + expect(contains(objInput, "age")).toBe(true); + + const edgeCaseInput = { 1: "one", "": "empty" }; + expect(contains(edgeCaseInput, "")).toBe(true); +}); // Given an object with properties // When passed to contains with a non-existent property name // Then it should return false +test("Given an object with properties, when passed to contains with a non-existent property name, returns false", () => { + const objInput = { name: "Peter", age: 48 }; + expect(contains(objInput, "gender")).toBe(false); + + const objInput1= { name: "Tayo", age: 10 }; + expect(contains(objInput1, "location")).toBe(false); +}); // Given invalid parameters like an array // When passed to contains // Then it should return false or throw an error +test("Given invalid parameters, contains throws a TypeError", () => { + const invalidInputs = ["hello", null, undefined, 123, true, [], () => {}]; + + invalidInputs.forEach(input => { + expect(() => contains(input, "a")).toThrow( + new TypeError("Invalid input: obj must be a plain object") + ); + }); +}); + + diff --git a/Sprint-2/implement/lookup.js b/Sprint-2/implement/lookup.js index a6746e07f..a8131fd98 100644 --- a/Sprint-2/implement/lookup.js +++ b/Sprint-2/implement/lookup.js @@ -1,4 +1,10 @@ -function createLookup() { +function createLookup(arrayLookUp) { + const objLookUp = {}; + + for (const [country, currency] of arrayLookUp) { + objLookUp[country] = currency; + } + return objLookUp; // implementation here } diff --git a/Sprint-2/implement/lookup.test.js b/Sprint-2/implement/lookup.test.js index 547e06c5a..a536af982 100644 --- a/Sprint-2/implement/lookup.test.js +++ b/Sprint-2/implement/lookup.test.js @@ -1,6 +1,16 @@ 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 currencyArray = [ + ["US", "USD"], + ["CA", "CAD"], + ]; + const expectedResult = { + US: "USD", + CA: "CAD", + }; + expect(createLookup(currencyArray)).toEqual(expectedResult); +}); /* diff --git a/Sprint-2/implement/querystring.js b/Sprint-2/implement/querystring.js index 45ec4e5f3..b496db76c 100644 --- a/Sprint-2/implement/querystring.js +++ b/Sprint-2/implement/querystring.js @@ -1,12 +1,33 @@ function parseQueryString(queryString) { + if (typeof queryString !== "string") { + throw new TypeError("Invalid input: Input must be a string"); + } const queryParams = {}; if (queryString.length === 0) { return queryParams; } + const keyValuePairs = queryString.split("&"); for (const pair of keyValuePairs) { - const [key, value] = pair.split("="); + const index = pair.indexOf("="); + let key, value; + + if (index === -1) { + key = pair; + value = ""; + } else { + key = pair.slice(0, index); + value = pair.slice(index + 1); + } + + try { + key = decodeURIComponent(key); + value = decodeURIComponent(value); + } catch (err) { + throw new Error("Invalid URL-encoded string"); + } + queryParams[key] = value; } diff --git a/Sprint-2/implement/querystring.test.js b/Sprint-2/implement/querystring.test.js index 3e218b789..5086ac04b 100644 --- a/Sprint-2/implement/querystring.test.js +++ b/Sprint-2/implement/querystring.test.js @@ -3,10 +3,82 @@ // 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("handles empty query string", () => { + expect(parseQueryString("")).toEqual({}); +}); + +test("handles empty key", () => { + expect(parseQueryString("=value")).toEqual({ + "": "value", + }); +}); + +test("parses multiple key-value pairs", () => { + expect(parseQueryString("a=1&b=2&c=3")).toEqual({ + a: "1", + b: "2", + c: "3", + }); +}); + +test("parses values with spaces or special characters", () => { + expect(parseQueryString("name=John%20Doe&city=New+York")).toEqual({ + name: "John Doe", + city: "New+York", + }); + expect(parseQueryString("tags%5B%5D=hello%20world")).toEqual({ + "tags[]": "hello world", + }); +}); + +test("throws TypeError for non-string input", () => { + expect(() => parseQueryString(null)).toThrow(TypeError); + expect(() => parseQueryString(null)).toThrow( + "Invalid input: Input must be a string" + ); + + expect(() => parseQueryString(undefined)).toThrow(TypeError); + expect(() => parseQueryString(undefined)).toThrow( + "Invalid input: Input must be a string" + ); + + expect(() => parseQueryString(123)).toThrow(TypeError); + expect(() => parseQueryString(123)).toThrow( + "Invalid input: Input must be a string" + ); + + expect(() => parseQueryString({})).toThrow(TypeError); + expect(() => parseQueryString({})).toThrow( + "Invalid input: Input must be a string" + ); +}); + +test("handles repeated keys, last value wins", () => { + expect(parseQueryString("a=1&a=2")).toEqual({ + a: "2", + }); +}); + +test("handles trailing '&'", () => { + expect(parseQueryString("a=1&b=2&")).toEqual({ + a: "1", + b: "2", + "": "", // empty pair after trailing & + }); +}); + +test("handles leading '&'", () => { + expect(parseQueryString("&a=1&b=2")).toEqual({ + "": "", // empty pair before leading & + a: "1", + b: "2", }); }); diff --git a/Sprint-2/implement/tally.js b/Sprint-2/implement/tally.js index f47321812..65263d045 100644 --- a/Sprint-2/implement/tally.js +++ b/Sprint-2/implement/tally.js @@ -1,3 +1,19 @@ -function tally() {} +function tally(arr) { + if (!Array.isArray(arr)) { + throw new TypeError("Invalid input: Input must be an array"); + } + + const result = Object.create(null); + + for (const item of arr) { + if (result[item]) { + result[item] += 1; + } 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..ca94edf40 100644 --- a/Sprint-2/implement/tally.test.js +++ b/Sprint-2/implement/tally.test.js @@ -19,16 +19,33 @@ const tally = require("./tally.js"); // Given a function called tally // When passed an array of items // Then it should return an object containing the count for each unique item +test("tally when passed an array of items, returns an object containing counts for each unique item", () => { + expect(tally(["a"])).toEqual({ a: 1 }); +}); // 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("when an array with duplicate items is passed to tally", () => { + expect(tally(["a", "a", "b", "c"])).toEqual({ a: 2, b: 1, c: 1 }); +}); // Given an invalid input like a string // When passed to tally // Then it should throw an error +test("when an invalid input is passed to tally, throws an error", () => { + expect(() => tally("house")).toThrow( + new TypeError("Invalid input: Input must be an array") + ); +}); + +test("tally handles items that match inherited object properties", () => { + expect(tally(["toString", "toString"])).toEqual({ toString: 2 }); +}); diff --git a/Sprint-2/interpret/invert.js b/Sprint-2/interpret/invert.js index bb353fb1f..da315753a 100644 --- a/Sprint-2/interpret/invert.js +++ b/Sprint-2/interpret/invert.js @@ -8,22 +8,28 @@ function invert(obj) { const invertedObj = {}; - + if (Object.prototype.toString.call(obj) !== "[object Object]") { + throw new TypeError("Invalid input: Input must be a plain object"); + } 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 } +module.exports = invert; +// a) What is the current return value when invert is called with { a : 1 } +// Answer: { key: 1} // b) What is the current return value when invert is called with { a: 1, b: 2 } - +// Answer: { key:2} // c) What is the target return value when invert is called with {a : 1, b: 2} - -// c) What does Object.entries return? Why is it needed in this program? - -// d) Explain why the current return value is different from the target output - -// e) Fix the implementation of invert (and write tests to prove it's fixed!) +// Answer: { '1': 'a', '2': 'b' } +// d) What does Object.entries return? Why is it needed in this program? +// Object.entries(obj) returns an array of [key, value] pairs, like: [ [ 'a', 1 ], [ 'b', 2 ] ] +// e) Explain why the current return value is different from the target output +// Answer: The current return value is different from the target output because `invertedObj.key` +// uses the literal string "key" instead of the variable key. To use the variable's value as the property name, +// we must use bracket notation: invertedObj[value] = key. +// f) Fix the implementation of invert (and write tests to prove it's fixed!) diff --git a/Sprint-2/interpret/invert.test.js b/Sprint-2/interpret/invert.test.js new file mode 100644 index 000000000..9ce03a6cd --- /dev/null +++ b/Sprint-2/interpret/invert.test.js @@ -0,0 +1,47 @@ +const invert = require("./invert.js"); + +test("Given an empty object, return an empty object", () => { + expect(invert({})).toEqual({}); +}); + +test("Given a single key-value pair object, returns a swap value", () => { + expect(invert({ a: 1 })).toEqual({ 1: "a" }); + expect(invert({ 1: 1 })).toEqual({ 1: "1" }); + expect(invert({ key: true })).toEqual({ true: "key" }); +}); + +test("Given multiple key-value pairs object, returns a swap value", () => { + expect(invert({ a: 1, b: 2 })).toEqual({ 1: "a", 2: "b" }); + expect(invert({ 1: "a", 2: "b" })).toEqual({ a: "1", b: "2" }); + expect(invert({ a: 1, b: "x", c: true })).toEqual({ + 1: "a", + x: "b", + true: "c", + }); + expect(invert({ a: 1, b: 1 })).toEqual({ 1: "b" }); + expect(invert({ a: 1, b: 1, c: 1 })).toEqual({ 1: "c" }); +}); + +test("Given an input that is not a plain object, throws an error", () => { + expect(() => invert(null)).toThrowError( + new TypeError("Invalid input: Input must be a plain object") + ); + expect(() => invert(undefined)).toThrowError( + new TypeError("Invalid input: Input must be a plain object") + ); + expect(() => invert(true)).toThrowError( + new TypeError("Invalid input: Input must be a plain object") + ); + expect(() => invert(null)).toThrowError( + new TypeError("Invalid input: Input must be a plain object") + ); +}); + +test("handles nested objects and arrays as values", () => { + const nestedObj = { a: { x: 1 } }; + const nestedArray = { b: [1, 2, 3] }; + // Nested object key becomes "[object Object]" + expect(invert(nestedObj)).toEqual({ "[object Object]": "a" }); + // Array key becomes "1,2,3" + expect(invert(nestedArray)).toEqual({ "1,2,3": "b" }); +}); \ No newline at end of file