From 06d7f12e30d4bd4aedf8e24beffe871f5de9ba1b Mon Sep 17 00:00:00 2001 From: Shayida Date: Fri, 21 Nov 2025 23:55:09 +0000 Subject: [PATCH] completed all tasks from Sprint 2 --- Sprint-2/debug/address.js | 2 +- Sprint-2/debug/author.js | 4 ++-- Sprint-2/debug/recipe.js | 8 ++++--- Sprint-2/implement/contains.js | 21 +++++++++++++++-- Sprint-2/implement/contains.test.js | 29 ++++++++++++++++++++++- Sprint-2/implement/lookup.js | 14 ++++++++++- Sprint-2/implement/lookup.test.js | 24 ++++++++++++++++++- Sprint-2/implement/querystring.js | 22 ++++++++++++++---- Sprint-2/implement/querystring.test.js | 32 ++++++++++++++++++++++++++ Sprint-2/implement/tally.js | 27 +++++++++++++++++++++- Sprint-2/implement/tally.test.js | 18 +++++++++++++++ Sprint-2/interpret/invert.js | 14 +++++++++++ 12 files changed, 199 insertions(+), 16 deletions(-) diff --git a/Sprint-2/debug/address.js b/Sprint-2/debug/address.js index 940a6af83..f28985431 100644 --- a/Sprint-2/debug/address.js +++ b/Sprint-2/debug/address.js @@ -12,4 +12,4 @@ const address = { postcode: "XYZ 123", }; -console.log(`My house number is ${address[0]}`); +console.log(`My house number is ${address.houseNumber}`); \ No newline at end of file diff --git a/Sprint-2/debug/author.js b/Sprint-2/debug/author.js index 8c2125977..2ef4550d2 100644 --- a/Sprint-2/debug/author.js +++ b/Sprint-2/debug/author.js @@ -11,6 +11,6 @@ const author = { alive: true, }; -for (const value of author) { +for (const value of of Object.values(author)) { console.log(value); -} +} \ No newline at end of file diff --git a/Sprint-2/debug/recipe.js b/Sprint-2/debug/recipe.js index 6cbdd22cd..3c9206dfc 100644 --- a/Sprint-2/debug/recipe.js +++ b/Sprint-2/debug/recipe.js @@ -10,6 +10,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); +} \ No newline at end of file diff --git a/Sprint-2/implement/contains.js b/Sprint-2/implement/contains.js index cd779308a..943f72b3c 100644 --- a/Sprint-2/implement/contains.js +++ b/Sprint-2/implement/contains.js @@ -1,3 +1,20 @@ -function contains() {} +function contains(obj, prop) { + if (!obj || typeof obj !== "object" || Array.isArray(obj)){ -module.exports = contains; + return false; + + } +} + +if (obj.hasOwnProperty(prop)){ + + return true; + + } + else{ + + return false; + } + + +module.exports = contains; \ No newline at end of file diff --git a/Sprint-2/implement/contains.test.js b/Sprint-2/implement/contains.test.js index 326bdb1f2..ee2c57696 100644 --- a/Sprint-2/implement/contains.test.js +++ b/Sprint-2/implement/contains.test.js @@ -20,16 +20,43 @@ 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.todo("contains on empty object returns false"); +test("contains on empty object returns false", () =>{ + + const result = contains({}, "a"); + expect(result).toBe(false); + + }); // Given an object with properties // When passed to contains with an existing property name // Then it should return true +test("returns true when the property is there", () =>{ + const obj = { a: 1, b: 2 }; + const result = contains(obj, "a"); + expect(result).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 when the property isn't there", ()=>{ + + const obj = { a: 1, b: 2 }; + const result = contains(obj,"c"); + expect(result).toBe(false); + + }); // Given invalid parameters like an array // When passed to contains // Then it should return false or throw an error + +test("invalid stuff like arrays or numbers should just return false", () =>{ + + expect(contains([], "a")).toBe(false); + expect(contains(null, "x")).toBe(false); + expect(contains(123, "nope")).tobe(false); + + }); \ No newline at end of file diff --git a/Sprint-2/implement/lookup.js b/Sprint-2/implement/lookup.js index a6746e07f..0a29e8c72 100644 --- a/Sprint-2/implement/lookup.js +++ b/Sprint-2/implement/lookup.js @@ -1,5 +1,17 @@ -function createLookup() { +function createLookup(pairs) { // implementation here + + const lookup = {}; + + for (let i = 0; i < pairs.length; i++){ + + const country = pairs[i][0]; + const country = pairs[i][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..fc94051c3 100644 --- a/Sprint-2/implement/lookup.test.js +++ b/Sprint-2/implement/lookup.test.js @@ -1,6 +1,28 @@ const createLookup = require("./lookup.js"); -test.todo("creates a country currency code lookup for multiple codes"); +//test.todo("creates a country currency code lookup for multiple codes"); +test("creates a country currency code lookup for multiple codes", () =>{ + const countryCurrencyPairs =[ + + ['US', 'USD'], + ['CA', 'CAD'], + ['JP', 'JPY'] + + ]; + + const expected ={ + + 'US': 'USD', + 'CA': 'CAD', + 'JP': 'JPY' + + }; + + const result = createLookup(countryCurrencyPairs); + + expect(result).toEqual(expected); + +}); /* diff --git a/Sprint-2/implement/querystring.js b/Sprint-2/implement/querystring.js index 45ec4e5f3..2d84c5392 100644 --- a/Sprint-2/implement/querystring.js +++ b/Sprint-2/implement/querystring.js @@ -4,13 +4,27 @@ function parseQueryString(queryString) { return queryParams; } const keyValuePairs = queryString.split("&"); + for (let i = 0; i < keyValuePairs.length; i++){ + const pair = keyValuePairs[i]; + const indexOfEquals = pair.indexOf("="); + if(indexOfEquals === -1){ + } + else{ + const key = pair.slice(0, indexOfEquals); + const value = pair.slice(indexOfEquals + 1); + queryParams[key] = value; + } + } + + - for (const pair of keyValuePairs) { - const [key, value] = pair.split("="); - queryParams[key] = value; + + //for (const pair of keyValuePairs) { + //const [key, value] = pair.split("="); + //queryParams[key] = value; } return queryParams; -} + module.exports = parseQueryString; diff --git a/Sprint-2/implement/querystring.test.js b/Sprint-2/implement/querystring.test.js index 3e218b789..960444e0b 100644 --- a/Sprint-2/implement/querystring.test.js +++ b/Sprint-2/implement/querystring.test.js @@ -10,3 +10,35 @@ test("parses querystring values containing =", () => { "equation": "x=y+1", }); }); + +test("parses multiple key=value pairs",() =>{ + expect(parseQueryString("a=1&b=2&c=3")).toEqual({ + + a: "1", + b: "2", + c: "3", + + }); +}); + +test("handles empty querystring",() =>{ + + expect(parseQueryString("")).toEqual({}); +}); + + +test("handles key with no value",() =>{ + + expect(parseQueryString("foo")).toEqual({ foo:""}); +}); + + +test("handles mix of empty and non-empty values",() =>{ + + expect(parseQueryString("foo=&bar=42")).toEqual({ + + foo: "", + bar: "42", + + }); +}); \ No newline at end of file diff --git a/Sprint-2/implement/tally.js b/Sprint-2/implement/tally.js index f47321812..62b8b576d 100644 --- a/Sprint-2/implement/tally.js +++ b/Sprint-2/implement/tally.js @@ -1,3 +1,28 @@ -function tally() {} +function tally(array) { + + if (!Array.isArray(arr)) { + throw new Error("tally expects an array!"); + } + + const counts = {}; + + if (arr.length === 0){ + + return counts; + } + for (let i = 0; i < arr.length; i++){ + + const item = arr[i]; + + if (counts[item]) { + + 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..e23c46b46 100644 --- a/Sprint-2/implement/tally.test.js +++ b/Sprint-2/implement/tally.test.js @@ -25,10 +25,28 @@ const tally = require("./tally.js"); // 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 counts items correctly",() =>{ + + 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("tally throws error on non-array input",() =>{ + + expect(() =>tally("not an array")).toThrow(); + }); diff --git a/Sprint-2/interpret/invert.js b/Sprint-2/interpret/invert.js index bb353fb1f..3a6574e32 100644 --- a/Sprint-2/interpret/invert.js +++ b/Sprint-2/interpret/invert.js @@ -17,13 +17,27 @@ function invert(obj) { } // 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? +[["a", 1], ["b", 2]] // d) Explain why the current return value is different from the target output +Object.entries gives you: the key and the value,So we can swap them easily. // e) Fix the implementation of invert (and write tests to prove it's fixed!) +function invert(obj) { + const invertedObj = {}; + + for (const [key, value] of Object.entries(obj)) { + invertedObj.key = value; + } + + return invertedObj; +} \ No newline at end of file