From b352ef3a4e62b7bc8d25751de401d7b9f58f97ed Mon Sep 17 00:00:00 2001 From: sheida-shab Date: Sat, 5 Jul 2025 13:29:52 +0100 Subject: [PATCH 01/13] fixing the errors in debug folder --- Sprint-2/debug/address.js | 2 +- Sprint-2/debug/author.js | 4 ++-- Sprint-2/debug/recipe.js | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Sprint-2/debug/address.js b/Sprint-2/debug/address.js index 940a6af83..36d2f865d 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}`); diff --git a/Sprint-2/debug/author.js b/Sprint-2/debug/author.js index 8c2125977..5ea669461 100644 --- a/Sprint-2/debug/author.js +++ b/Sprint-2/debug/author.js @@ -10,7 +10,7 @@ const author = { age: 40, alive: true, }; - -for (const value of author) { +//console.log(author); +for (const value in author) { console.log(value); } diff --git a/Sprint-2/debug/recipe.js b/Sprint-2/debug/recipe.js index 6cbdd22cd..d0b246f22 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}`); From 3114b737f7e1ec07245b4ac89179b3bbd89b8df9 Mon Sep 17 00:00:00 2001 From: sheida-shab Date: Wed, 9 Jul 2025 19:37:48 +0100 Subject: [PATCH 02/13] Implement function contains and write test cases --- Sprint-2/implement/contains.js | 11 ++++++++++- Sprint-2/implement/contains.test.js | 24 +++++++++++++++++++++++- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/Sprint-2/implement/contains.js b/Sprint-2/implement/contains.js index cd779308a..34ff1dc11 100644 --- a/Sprint-2/implement/contains.js +++ b/Sprint-2/implement/contains.js @@ -1,3 +1,12 @@ -function contains() {} +function contains(inputObject,inputProperty) { + if ( + inputObject === null || + typeof inputObject !== "object" || + Array.isArray(inputObject) + ) { + return false; + } + return inputObject.hasOwnProperty(inputProperty); +} module.exports = contains; diff --git a/Sprint-2/implement/contains.test.js b/Sprint-2/implement/contains.test.js index 326bdb1f2..8b80d3816 100644 --- a/Sprint-2/implement/contains.test.js +++ b/Sprint-2/implement/contains.test.js @@ -20,16 +20,38 @@ 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 obj = {}; + const result = contains(obj, "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("object contains an existing property name", () => { + 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("object contains with a non-existent property name", () => { + 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("Given invalid parameters like an array",() => { + const obj = ("a",1,"b", 2 ); + const result = contains(obj, "a"); + expect(result).toBe(false); +}); From 7d0141c09f30eb20eea8efe7e008409c57cf4b40 Mon Sep 17 00:00:00 2001 From: sheida-shab Date: Wed, 9 Jul 2025 19:41:36 +0100 Subject: [PATCH 03/13] change input obj in a test --- Sprint-2/implement/contains.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sprint-2/implement/contains.test.js b/Sprint-2/implement/contains.test.js index 8b80d3816..feb504918 100644 --- a/Sprint-2/implement/contains.test.js +++ b/Sprint-2/implement/contains.test.js @@ -51,7 +51,7 @@ test("object contains with a non-existent property name", () => { // When passed to contains // Then it should return false or throw an error test("Given invalid parameters like an array",() => { - const obj = ("a",1,"b", 2 ); + const obj = ["a",1,"b", 2 ]; const result = contains(obj, "a"); expect(result).toBe(false); }); From a5a7c8dd1f826972d9b6380b6639bcaa36722288 Mon Sep 17 00:00:00 2001 From: sheida-shab Date: Wed, 9 Jul 2025 20:43:11 +0100 Subject: [PATCH 04/13] Implement createLookup function and write some test cases --- Sprint-2/implement/lookup.js | 15 ++++++++--- Sprint-2/implement/lookup.test.js | 45 ++++++++++++++++++++++++++++++- 2 files changed, 56 insertions(+), 4 deletions(-) diff --git a/Sprint-2/implement/lookup.js b/Sprint-2/implement/lookup.js index a6746e07f..4a2b2c0cd 100644 --- a/Sprint-2/implement/lookup.js +++ b/Sprint-2/implement/lookup.js @@ -1,5 +1,14 @@ -function createLookup() { - // implementation here + +function createLookup(countryCurrArray) { + if ( + !Array.isArray(countryCurrArray) || + !countryCurrArray.every((item) => Array.isArray(item)) + ) { + return false; + } + return Object.fromEntries(countryCurrArray); + + } -module.exports = createLookup; +module.exports = createLookup; \ No newline at end of file diff --git a/Sprint-2/implement/lookup.test.js b/Sprint-2/implement/lookup.test.js index 547e06c5a..f9fb449e6 100644 --- a/Sprint-2/implement/lookup.test.js +++ b/Sprint-2/implement/lookup.test.js @@ -1,6 +1,49 @@ 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 array = [ + ["US", "USD"], + ["CA", "CAD"], + ["GB", "GBP"], + ]; + const result = createLookup(array); + expect(result).toEqual({ + US: "USD", + CA: "CAD", + GB: "GBP", + }); +} +); + +test("input array is empty", () => { + const array = []; + const result = createLookup(array); + expect(result).toEqual({}); +}); + +test("The input is an array but doesn't contain array elements", () => { + const array = [1,2,3,4]; + const result = createLookup(array); + expect(result).toEqual(false); +}); + +test("ُThe input is not an array", () => { + const array ="US-USD" ; + const result = createLookup(array); + expect(result).toEqual(false); +}); + +test("ُThe input is an array of strings", () => { + const array = ["US:USD","CA:CAD"]; + const result = createLookup(array); + expect(result).toEqual(false); +}); + +test("creates a country currency code lookup for one array", () => { + const array = [["US", "USD"]]; + const result = createLookup(array); + expect(result).toEqual({US: "USD"}); +}); /* From 012982d889e46b8854105811bc4f746bb3f74420 Mon Sep 17 00:00:00 2001 From: sheida-shab Date: Wed, 9 Jul 2025 23:13:30 +0100 Subject: [PATCH 05/13] fix the bug of parseQueryString function and pass the test --- Sprint-2/implement/querystring.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Sprint-2/implement/querystring.js b/Sprint-2/implement/querystring.js index 45ec4e5f3..f00834174 100644 --- a/Sprint-2/implement/querystring.js +++ b/Sprint-2/implement/querystring.js @@ -6,8 +6,9 @@ function parseQueryString(queryString) { const keyValuePairs = queryString.split("&"); for (const pair of keyValuePairs) { - const [key, value] = pair.split("="); - queryParams[key] = value; + const [key, ...value] = pair.split("="); + const strvalue = value.join("="); + queryParams[key] = strvalue; } return queryParams; From 47893ce5a84bf33ffa4bf7b66b2cdeefce9afde9 Mon Sep 17 00:00:00 2001 From: sheida-shab Date: Wed, 9 Jul 2025 23:56:41 +0100 Subject: [PATCH 06/13] Add more test cases --- Sprint-2/implement/querystring.test.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/Sprint-2/implement/querystring.test.js b/Sprint-2/implement/querystring.test.js index 3e218b789..4b1e1dde3 100644 --- a/Sprint-2/implement/querystring.test.js +++ b/Sprint-2/implement/querystring.test.js @@ -10,3 +10,28 @@ test("parses querystring values containing =", () => { "equation": "x=y+1", }); }); + +test("parses querystring values contains one pair", () => { + expect(parseQueryString("brand=Tesla")).toEqual({ + brand: "Tesla", + }); +}); + +test("parses querystring contains more than one pair ",() =>{ + expect(parseQueryString("sort=lowest&colour=yellow")).toEqual({ + "sort":"lowest", + "colour":"yellow"}); + }); + + test("parses empty querystring", () => { + expect(parseQueryString("")).toEqual({}); + }); + + test("parses key without value", () => { + expect(parseQueryString("name")).toEqual({name:""}); + }); + + + test("parses key with empty value", () => { + expect(parseQueryString("name=")).toEqual({ name: "" }); + }); From 72cab09ec88d605a0b972c79fa0faa4e267202ef Mon Sep 17 00:00:00 2001 From: sheida-shab Date: Thu, 10 Jul 2025 00:44:16 +0100 Subject: [PATCH 07/13] implement function tally and write test cases --- Sprint-2/implement/tally.js | 20 +++++++++++++++++++- Sprint-2/implement/tally.test.js | 19 ++++++++++++++++++- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/Sprint-2/implement/tally.js b/Sprint-2/implement/tally.js index f47321812..7c3a43806 100644 --- a/Sprint-2/implement/tally.js +++ b/Sprint-2/implement/tally.js @@ -1,3 +1,21 @@ -function tally() {} +function tally(itemArray) { + if (itemArray.length == 0 ) { + return {}; + }else{ + if(typeof itemArray==="string"){ + throw new Error("invalid input"); + } + } + let countArray={}; + for(const item of itemArray ){ + if(countArray.hasOwnProperty(item)){ + countArray[item]++; + }else{ + countArray[item]=1; + } + } + return countArray; +} module.exports = tally; + \ No newline at end of file diff --git a/Sprint-2/implement/tally.test.js b/Sprint-2/implement/tally.test.js index 2ceffa8dd..0274521ac 100644 --- a/Sprint-2/implement/tally.test.js +++ b/Sprint-2/implement/tally.test.js @@ -23,12 +23,29 @@ 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 on an array with duplicate items returns counts for each unique item",()=>{ + expect(tally(["a", "a", "a","b"])).toEqual({a:3,b:1}); + + } +); // Given an invalid input like a string // When passed to tally // Then it should throw an error +test("tally on an invalid input like a string should throw an error",()=>{ + expect(()=>tally("String Test")).toThrow("invalid input") +}); + +test("tally on an array with various input", () => { + expect(tally(["a"])).toEqual({ a: 1 }); + expect(tally(["a", "a", "b", "c"])).toEqual({ a: 2, b: 1, c: 1 }); +}); + \ No newline at end of file From 1a45dec29a6d511b22d9f261c1985256e395db5a Mon Sep 17 00:00:00 2001 From: sheida-shab Date: Thu, 10 Jul 2025 18:07:28 +0100 Subject: [PATCH 08/13] Answer questions and modify and test the function --- Sprint-2/interpret/invert.js | 13 +++++++++++-- Sprint-2/interpret/invert.test.js | 9 +++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) create mode 100644 Sprint-2/interpret/invert.test.js diff --git a/Sprint-2/interpret/invert.js b/Sprint-2/interpret/invert.js index bb353fb1f..fd9f3d578 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; } +module.exports = invert; // a) What is the current return value when invert is called with { a : 1 } +//{key : 1} because invertedObj.key= value add a property with the exact name "key" and value "1" to invertedObj. + // b) What is the current 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} +//{key : 2} because first the function add {key:1} to invertedObj and then modify the property and assign value 2 to key "key". +// 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? +//It returns pairs of keys and values as an Array of pairs. + // d) Explain why the current return value is different from the target output +//Because invertedObj.key means object "invertedObj" has a key property with the exact name "key" and we need to add a key that its name is the value of key property. + // e) 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..780716239 --- /dev/null +++ b/Sprint-2/interpret/invert.test.js @@ -0,0 +1,9 @@ +const invert = require("./invert.js"); + +test("invert an object with one pair",()=>{ + expect(invert({a:1})).toEqual({1:"a"}); +}); + +test("invert an object with more than ne pair", () => { + expect(invert({ a: 1 , b : 2})).toEqual({ 1: "a" , 2: "b"}); +}); From d3b69b27ca3fe6ba78892e8c74da69f4852551d1 Mon Sep 17 00:00:00 2001 From: sheida-shab Date: Sat, 12 Jul 2025 23:18:17 +0100 Subject: [PATCH 09/13] fixing the problem --- Sprint-2/debug/author.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Sprint-2/debug/author.js b/Sprint-2/debug/author.js index 5ea669461..f035f3104 100644 --- a/Sprint-2/debug/author.js +++ b/Sprint-2/debug/author.js @@ -11,6 +11,6 @@ const author = { alive: true, }; //console.log(author); -for (const value in author) { - console.log(value); +for (const key in author) { + console.log(author[key]); } From 36ba9949f2ddc2185fc93a1aee88751b88c76ed1 Mon Sep 17 00:00:00 2001 From: sheida-shab Date: Sat, 12 Jul 2025 23:43:00 +0100 Subject: [PATCH 10/13] print each ingredient in a seperate line --- Sprint-2/debug/recipe.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/Sprint-2/debug/recipe.js b/Sprint-2/debug/recipe.js index d0b246f22..dc17ba737 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.ingredients}`); +console.log(`${recipe.title} serves ${recipe.serves}`); +console.log("ingredients:"); +for(const item of recipe.ingredients){ + console.log(item); +} \ No newline at end of file From 126aed515d6d7440ebc54597c9fbbfc3545b1b98 Mon Sep 17 00:00:00 2001 From: sheida-shab Date: Sun, 13 Jul 2025 00:14:48 +0100 Subject: [PATCH 11/13] added a test to confirm arrays return false --- Sprint-2/implement/contains.test.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Sprint-2/implement/contains.test.js b/Sprint-2/implement/contains.test.js index feb504918..6b5b65f95 100644 --- a/Sprint-2/implement/contains.test.js +++ b/Sprint-2/implement/contains.test.js @@ -55,3 +55,8 @@ test("Given invalid parameters like an array",() => { const result = contains(obj, "a"); expect(result).toBe(false); }); +test("Given invalid parameters like an array2", () => { + const obj = ["a", 1, "b", 2]; + const result = contains(obj, "1"); + expect(result).toBe(false); +}); \ No newline at end of file From bb68239a73cd0a525c60f40585b2e766e9ef7408 Mon Sep 17 00:00:00 2001 From: sheida-shab Date: Sun, 13 Jul 2025 00:26:48 +0100 Subject: [PATCH 12/13] return null instead of false , fix ed function and test --- Sprint-2/implement/lookup.js | 2 +- Sprint-2/implement/lookup.test.js | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Sprint-2/implement/lookup.js b/Sprint-2/implement/lookup.js index 4a2b2c0cd..4d98cfc8c 100644 --- a/Sprint-2/implement/lookup.js +++ b/Sprint-2/implement/lookup.js @@ -4,7 +4,7 @@ function createLookup(countryCurrArray) { !Array.isArray(countryCurrArray) || !countryCurrArray.every((item) => Array.isArray(item)) ) { - return false; + return null; } return Object.fromEntries(countryCurrArray); diff --git a/Sprint-2/implement/lookup.test.js b/Sprint-2/implement/lookup.test.js index f9fb449e6..9df2f87dc 100644 --- a/Sprint-2/implement/lookup.test.js +++ b/Sprint-2/implement/lookup.test.js @@ -24,19 +24,19 @@ test("input array is empty", () => { test("The input is an array but doesn't contain array elements", () => { const array = [1,2,3,4]; const result = createLookup(array); - expect(result).toEqual(false); + expect(result).toEqual(null); }); test("ُThe input is not an array", () => { const array ="US-USD" ; const result = createLookup(array); - expect(result).toEqual(false); + expect(result).toEqual(null); }); test("ُThe input is an array of strings", () => { const array = ["US:USD","CA:CAD"]; const result = createLookup(array); - expect(result).toEqual(false); + expect(result).toEqual(null); }); test("creates a country currency code lookup for one array", () => { From fcb237a97060db9e8d50ef230403afebcc763daa Mon Sep 17 00:00:00 2001 From: sheida-shab Date: Sun, 13 Jul 2025 00:49:54 +0100 Subject: [PATCH 13/13] change the code to handle more input situation --- Sprint-2/implement/tally.js | 26 ++++++++++++++------------ Sprint-2/implement/tally.test.js | 5 +++++ Sprint-2/package-lock.json | 18 +++++++++++++++++- Sprint-2/package.json | 3 ++- 4 files changed, 38 insertions(+), 14 deletions(-) diff --git a/Sprint-2/implement/tally.js b/Sprint-2/implement/tally.js index 7c3a43806..44ee5e3e4 100644 --- a/Sprint-2/implement/tally.js +++ b/Sprint-2/implement/tally.js @@ -1,21 +1,23 @@ function tally(itemArray) { + if (!Array.isArray(itemArray)) { + throw new Error("invalid input"); + } + if (itemArray.length == 0 ) { return {}; - }else{ - if(typeof itemArray==="string"){ - throw new Error("invalid input"); - } } + let countArray={}; - for(const item of itemArray ){ - if(countArray.hasOwnProperty(item)){ - countArray[item]++; - }else{ - countArray[item]=1; + for(const item of itemArray ){ + if(countArray.hasOwnProperty(item)){ + countArray[item]++; + }else{ + countArray[item]=1; + } } - } - return countArray; -} + return countArray; + } + module.exports = tally; \ No newline at end of file diff --git a/Sprint-2/implement/tally.test.js b/Sprint-2/implement/tally.test.js index 0274521ac..08765e4d9 100644 --- a/Sprint-2/implement/tally.test.js +++ b/Sprint-2/implement/tally.test.js @@ -44,6 +44,11 @@ test("tally on an invalid input like a string should throw an error",()=>{ expect(()=>tally("String Test")).toThrow("invalid input") }); +test("tally on an invalid input like null should throw an error",()=>{ + expect(()=>tally()).toThrow("invalid input") +}); + + test("tally on an array with various input", () => { expect(tally(["a"])).toEqual({ a: 1 }); expect(tally(["a", "a", "b", "c"])).toEqual({ a: 2, b: 1, c: 1 }); diff --git a/Sprint-2/package-lock.json b/Sprint-2/package-lock.json index 9b4c725d6..f19776e49 100644 --- a/Sprint-2/package-lock.json +++ b/Sprint-2/package-lock.json @@ -9,7 +9,8 @@ "version": "1.0.0", "license": "ISC", "devDependencies": { - "jest": "^29.7.0" + "jest": "^29.7.0", + "prettier": "^3.6.2" } }, "node_modules/@ampproject/remapping": { @@ -3195,6 +3196,21 @@ "node": ">=8" } }, + "node_modules/prettier": { + "version": "3.6.2", + "resolved": "https://registry.npmjs.org/prettier/-/prettier-3.6.2.tgz", + "integrity": "sha512-I7AIg5boAr5R0FFtJ6rCfD+LFsWHp81dolrFD8S79U9tb8Az2nGrJncnMSnys+bpQJfRUzqs9hnA81OAA3hCuQ==", + "dev": true, + "bin": { + "prettier": "bin/prettier.cjs" + }, + "engines": { + "node": ">=14" + }, + "funding": { + "url": "https://github.com/prettier/prettier?sponsor=1" + } + }, "node_modules/pretty-format": { "version": "29.7.0", "resolved": "https://registry.npmjs.org/pretty-format/-/pretty-format-29.7.0.tgz", diff --git a/Sprint-2/package.json b/Sprint-2/package.json index 80c16780f..a9c1082ae 100644 --- a/Sprint-2/package.json +++ b/Sprint-2/package.json @@ -10,6 +10,7 @@ "author": "", "license": "ISC", "devDependencies": { - "jest": "^29.7.0" + "jest": "^29.7.0", + "prettier": "^3.6.2" } }