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..f035f3104 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(value); +//console.log(author); +for (const key in author) { + console.log(author[key]); } diff --git a/Sprint-2/debug/recipe.js b/Sprint-2/debug/recipe.js index 6cbdd22cd..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}`); +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 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..6b5b65f95 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("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); +}); +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 diff --git a/Sprint-2/implement/lookup.js b/Sprint-2/implement/lookup.js index a6746e07f..4d98cfc8c 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 null; + } + 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..9df2f87dc 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(null); +}); + +test("ُThe input is not an array", () => { + const array ="US-USD" ; + const result = createLookup(array); + 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(null); +}); + +test("creates a country currency code lookup for one array", () => { + const array = [["US", "USD"]]; + const result = createLookup(array); + expect(result).toEqual({US: "USD"}); +}); /* 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; 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: "" }); + }); diff --git a/Sprint-2/implement/tally.js b/Sprint-2/implement/tally.js index f47321812..44ee5e3e4 100644 --- a/Sprint-2/implement/tally.js +++ b/Sprint-2/implement/tally.js @@ -1,3 +1,23 @@ -function tally() {} +function tally(itemArray) { + if (!Array.isArray(itemArray)) { + throw new Error("invalid input"); + } + + if (itemArray.length == 0 ) { + return {}; + } + + 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..08765e4d9 100644 --- a/Sprint-2/implement/tally.test.js +++ b/Sprint-2/implement/tally.test.js @@ -23,12 +23,34 @@ 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 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 }); +}); + \ No newline at end of file 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"}); +}); 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" } }