From 44ec0874406c57db4dce9c7d77beff73d4a7d010 Mon Sep 17 00:00:00 2001 From: HannaOdud Date: Sun, 9 Nov 2025 23:48:56 +0000 Subject: [PATCH] Sprint-2 --- Sprint-2/debug/address.js | 5 +++-- Sprint-2/debug/author.js | 5 +++-- Sprint-2/debug/recipe.js | 2 +- Sprint-2/implement/contains.js | 14 +++++++++++++- Sprint-2/implement/contains.test.js | 23 ++++++++++++++++++++++- Sprint-2/implement/lookup.js | 5 +++-- Sprint-2/implement/lookup.test.js | 9 ++++++++- Sprint-2/implement/querystring.js | 8 ++++++-- Sprint-2/implement/querystring.test.js | 3 +++ Sprint-2/implement/tally.js | 19 ++++++++++++++++++- Sprint-2/implement/tally.test.js | 12 +++++++++++- Sprint-2/interpret/invert.js | 16 +++++++++------- Sprint-2/interpret/invert.test.js | 11 +++++++++++ 13 files changed, 111 insertions(+), 21 deletions(-) create mode 100644 Sprint-2/interpret/invert.test.js diff --git a/Sprint-2/debug/address.js b/Sprint-2/debug/address.js index 940a6af83..aeaa93a6f 100644 --- a/Sprint-2/debug/address.js +++ b/Sprint-2/debug/address.js @@ -11,5 +11,6 @@ const address = { country: "England", postcode: "XYZ 123", }; - -console.log(`My house number is ${address[0]}`); +//the fixed two lines +console.log(`My house number is ${address.houseNumber}`); +console.log(`My house number is ${address["houseNumber"]}`); diff --git a/Sprint-2/debug/author.js b/Sprint-2/debug/author.js index 8c2125977..cd06b7ed1 100644 --- a/Sprint-2/debug/author.js +++ b/Sprint-2/debug/author.js @@ -10,7 +10,8 @@ const author = { age: 40, alive: true, }; - -for (const value of author) { +//changed code at line 14 +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..ed45c81a4 100644 --- a/Sprint-2/debug/recipe.js +++ b/Sprint-2/debug/recipe.js @@ -12,4 +12,4 @@ const recipe = { console.log(`${recipe.title} serves ${recipe.serves} ingredients: -${recipe}`); +${recipe.ingredients.join(", ")}`); diff --git a/Sprint-2/implement/contains.js b/Sprint-2/implement/contains.js index cd779308a..65c688728 100644 --- a/Sprint-2/implement/contains.js +++ b/Sprint-2/implement/contains.js @@ -1,3 +1,15 @@ -function contains() {} +function contains(object, property) { + if (Object.keys(object).length === 0 ){ + return false; + } + if (object.hasOwnProperty(property)){ + return true; + } + else{ + return false; + } + +} module.exports = contains; + diff --git a/Sprint-2/implement/contains.test.js b/Sprint-2/implement/contains.test.js index 326bdb1f2..92b5c79f1 100644 --- a/Sprint-2/implement/contains.test.js +++ b/Sprint-2/implement/contains.test.js @@ -20,16 +20,37 @@ 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("given an empty object, it should return false", () => { + const currentOutput = contains({}); + 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("given an object with properties, when passed to contains with an existing property name it should return true", () => { + const currentOutput = contains({a: 1, b: 2}, 'a') ; + 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("given an object with properties, when passed to contains with a non-existent property name it should return false", () => { + const currentOutput = contains({a: 1, b: 2}, 'c') ; + 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("given ivalid parameters like an array, when passed to contains it should return false throw an error", () => { + const currentOutput = contains([1,'a'], 'c') ; + const targetOutput = false; + expect(currentOutput).toEqual(targetOutput); +}); diff --git a/Sprint-2/implement/lookup.js b/Sprint-2/implement/lookup.js index a6746e07f..bb36e5fa8 100644 --- a/Sprint-2/implement/lookup.js +++ b/Sprint-2/implement/lookup.js @@ -1,5 +1,6 @@ -function createLookup() { - // implementation here +function createLookup(country_currency) { + let obj = Object.fromEntries(country_currency); + return obj } module.exports = createLookup; diff --git a/Sprint-2/implement/lookup.test.js b/Sprint-2/implement/lookup.test.js index 547e06c5a..a12bc492d 100644 --- a/Sprint-2/implement/lookup.test.js +++ b/Sprint-2/implement/lookup.test.js @@ -1,6 +1,13 @@ 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 currentOutput = createLookup([['US', 'USD'], ['CA', 'CAD']]) ; + const targetOutput = { + 'US': 'USD', + 'CA': 'CAD' + }; + expect(currentOutput).toEqual(targetOutput); +}); /* diff --git a/Sprint-2/implement/querystring.js b/Sprint-2/implement/querystring.js index 45ec4e5f3..0b1d3a60e 100644 --- a/Sprint-2/implement/querystring.js +++ b/Sprint-2/implement/querystring.js @@ -3,10 +3,14 @@ function parseQueryString(queryString) { if (queryString.length === 0) { return queryParams; } - const keyValuePairs = queryString.split("&"); + const keyValuePairs = queryString.split("&");//method split gives us an array for (const pair of keyValuePairs) { - const [key, value] = pair.split("="); + const indexForKey = pair.indexOf('='); + const key = pair.substring(0,indexForKey); + const value = pair.substring(indexForKey+1, pair.length) + + queryParams[key] = value; } diff --git a/Sprint-2/implement/querystring.test.js b/Sprint-2/implement/querystring.test.js index 3e218b789..871c7468a 100644 --- a/Sprint-2/implement/querystring.test.js +++ b/Sprint-2/implement/querystring.test.js @@ -10,3 +10,6 @@ test("parses querystring values containing =", () => { "equation": "x=y+1", }); }); +test("must return empty object if query string is empty", () => { + expect(parseQueryString("")).toEqual({ }); +}); diff --git a/Sprint-2/implement/tally.js b/Sprint-2/implement/tally.js index f47321812..61f0254e2 100644 --- a/Sprint-2/implement/tally.js +++ b/Sprint-2/implement/tally.js @@ -1,3 +1,20 @@ -function tally() {} +function tally(array) { + if (!Array.isArray(array)){ //check if array is array + throw new Error("Invalid input"); + } + if (array.length === 0 ){ + return {}; + } + const obj = {} + for (let item of array){ + if (obj[item]){ + obj[item]+=1; + } + else{ + obj[item]=1; + } + } + return obj +} module.exports = tally; diff --git a/Sprint-2/implement/tally.test.js b/Sprint-2/implement/tally.test.js index 2ceffa8dd..f671ab1af 100644 --- a/Sprint-2/implement/tally.test.js +++ b/Sprint-2/implement/tally.test.js @@ -23,12 +23,22 @@ 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.todo("tally on an empty array returns an empty object"); +test("Given an empty array, it should return 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("Given an array with duplicate items, it should return counts for each unique item ", () => { + 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("Given an invalid input like a string, it should throw an error", () => { + expect(() => { tally("?") }).toThrow("Invalid input"); +}); \ No newline at end of file diff --git a/Sprint-2/interpret/invert.js b/Sprint-2/interpret/invert.js index bb353fb1f..84e874eac 100644 --- a/Sprint-2/interpret/invert.js +++ b/Sprint-2/interpret/invert.js @@ -9,21 +9,23 @@ function invert(obj) { const invertedObj = {}; - for (const [key, value] of Object.entries(obj)) { - invertedObj.key = value; + for (const [key, value] of Object.entries(obj)) { //turn object into array of smaller arrays elements + invertedObj[value]= key; } return invertedObj; } +console.log(invert({ a: 1, b: 2 })) -// a) What is the current return value when invert is called with { a : 1 } +// 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 } +// 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} +// 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? +// c) What does Object.entries return? Why is it needed in this program? // turn object into array of smaller arrays elements -// d) Explain why the current return value is different from the target output +// d) Explain why the current return value is different from the target output // because we have to swap key and value at line 13 // e) Fix the implementation of invert (and write tests to prove it's fixed!) +module.exports = invert; \ No newline at end of file diff --git a/Sprint-2/interpret/invert.test.js b/Sprint-2/interpret/invert.test.js new file mode 100644 index 000000000..e87b19b8d --- /dev/null +++ b/Sprint-2/interpret/invert.test.js @@ -0,0 +1,11 @@ +const invert = require("./invert.js"); + +// Given an object +// When invert is passed this object +// Then it should swap the keys and values in the object + +// E.g. invert({x : 10, y : 20}), target output: {"10": "x", "20": "y"} + +test("Given an object, then it should swap the keys and values in the object ", () => { + expect( invert({x : 10, y : 20})).toEqual({"10": "x", "20": "y"}); +}); \ No newline at end of file