diff --git a/Sprint-2/debug/address.js b/Sprint-2/debug/address.js index 940a6af83..16f3dc7d2 100644 --- a/Sprint-2/debug/address.js +++ b/Sprint-2/debug/address.js @@ -1,4 +1,4 @@ -// Predict and explain first... +//Predict and explain first... // This code should log out the houseNumber from the address object // but it isn't working... @@ -12,4 +12,8 @@ const address = { postcode: "XYZ 123", }; -console.log(`My house number is ${address[0]}`); +console.log(`My house number is ${address.houseNumber}`); + +/* The code constructed well and the code is an object which is it is consist of key and value pair +in line 15 inside the console it is return in array method which is the code is not array so we have to change that +to object call "object.houseNumber" in this way it will work correctly */ diff --git a/Sprint-2/debug/author.js b/Sprint-2/debug/author.js index 8c2125977..86aca4efa 100644 --- a/Sprint-2/debug/author.js +++ b/Sprint-2/debug/author.js @@ -11,6 +11,10 @@ const author = { alive: true, }; -for (const value of author) { - console.log(value); +for (const property in author) { + console.log(`${property}:${author[property]}`); } + +/*In this code the problem is that the method for...of is used for array +not for object type instead we use for...in this will work through the object + and list the key and for value We use author[property] */ diff --git a/Sprint-2/debug/recipe.js b/Sprint-2/debug/recipe.js index 6cbdd22cd..2b79ee000 100644 --- a/Sprint-2/debug/recipe.js +++ b/Sprint-2/debug/recipe.js @@ -9,7 +9,12 @@ const recipe = { serves: 2, ingredients: ["olive oil", "tomatoes", "salt", "pepper"], }; +console.log(`${recipe.title} serves ${recipe.serves} +ingredients:`); +for (const element of recipe.ingredients) { + console.log(element); +} -console.log(`${recipe.title} serves ${recipe.serves} - ingredients: -${recipe}`); +/* In this code will log the recipe.title and recipe.serves but recipe.ingredient is not +represented well so it can't log the array ,to access and to get all the array index i used +for...of loop to log all the ingredient in the array*/ diff --git a/Sprint-2/implement/contains.js b/Sprint-2/implement/contains.js index cd779308a..978fb605b 100644 --- a/Sprint-2/implement/contains.js +++ b/Sprint-2/implement/contains.js @@ -1,3 +1,19 @@ -function contains() {} +function contains(targetObject, searchTerm) { + if ( + typeof targetObject !== "object" || + Object.keys(targetObject).length === 0 || + Array.isArray(targetObject) + ) { + return false; + } + + for (const property in targetObject) { + if (targetObject[property] === searchTerm || property === searchTerm) { + return true; + } + } + return false; +} +console.log(contains(["a", "b", "c"], "d")); module.exports = contains; diff --git a/Sprint-2/implement/contains.test.js b/Sprint-2/implement/contains.test.js index 326bdb1f2..ca8c2a9ef 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("The function should return undefined when an empty object is passed to it.", () => { + expect(contains({})).toBe(false); +}); // Given an object with properties // When passed to contains with an existing property name // Then it should return true +test("The function should return true when the object contains the targeted property", () => { + expect(contains({ town: "leeds" }, "leeds")).toBe(true); + expect(contains({ a: 1, b: 2, c: 3 }, "a")).toBe(true); + expect(contains({ name: "ahmed", sex: "male", age: 29 }, "male")).toBe(true); + expect(contains({ zone: 2, zone: 3, zone: 4 }, "zone")).toBe(true); +}); // Given an object with properties // When passed to contains with a non-existent property name // Then it should return false +test("the function should return false when the targeted property don't exist", () => { + expect(contains({ a: 1, b: 2, c: 3 }, "d")).toBe(false); + expect(contains({ year: 1998, month: 10, Date: 16 }, "age")).toBe(false); + expect(contains({ meal: "pizza", drink: "water", table: 3 }, "starter")).toBe( + false + ); +}); // Given invalid parameters like an array // When passed to contains // Then it should return false or throw an error +test("the function should return false when invalid parameter like an array is passed", () => { + expect(contains([10, 23, 34, 45], 45)).toBe(false); + expect(contains("hello", "hi")).toBe(false); + expect(contains(["green", "yellow", "rad"], "green")).toBe(false); + expect(contains(((30, 40, 50), 30))).toBe(false); +}); diff --git a/Sprint-2/implement/lookup.js b/Sprint-2/implement/lookup.js index a6746e07f..9193b567d 100644 --- a/Sprint-2/implement/lookup.js +++ b/Sprint-2/implement/lookup.js @@ -1,5 +1,12 @@ -function createLookup() { +function createLookup(arrayOfArrays) { // implementation here + const lookUpObject = {}; + for (const innerArray of arrayOfArrays) { + keyPair = innerArray[0]; + valuePair = innerArray[1]; + lookUpObject[keyPair] = valuePair; + } + return lookUpObject; } module.exports = createLookup; diff --git a/Sprint-2/implement/lookup.test.js b/Sprint-2/implement/lookup.test.js index 547e06c5a..2a710eb5a 100644 --- a/Sprint-2/implement/lookup.test.js +++ b/Sprint-2/implement/lookup.test.js @@ -1,6 +1,32 @@ 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", () => { + expect( + createLookup([ + ["US", "USD"], + ["CA", "CAD"], + ]) + ).toEqual({ + US: "USD", + CA: "CAD", + }); + expect( + createLookup([ + ["BE", "EUR"], + ["BR", "BRL"], + ["FI", "FIN"], + ]) + ).toEqual({ BE: "EUR", BR: "BRL", FI: "FIN" }); + expect( + createLookup([ + ["CV", "CPV"], + ["CN", "CHN"], + ["CR", "CRI"], + ["DE", "DEU"], + ["IN", "IND"], + ]) + ).toEqual({ CV: "CPV", CN: "CHN", CR: "CRI", DE: "DEU", IN: "IND" }); +}); /* diff --git a/Sprint-2/implement/querystring.js b/Sprint-2/implement/querystring.js index 45ec4e5f3..cc3af9cb4 100644 --- a/Sprint-2/implement/querystring.js +++ b/Sprint-2/implement/querystring.js @@ -6,11 +6,16 @@ function parseQueryString(queryString) { const keyValuePairs = queryString.split("&"); for (const pair of keyValuePairs) { - const [key, value] = pair.split("="); + const equalityPosition = pair.indexOf("="); + if (equalityPosition == -1) { + continue; + } + const key = pair.slice(0, equalityPosition); + const value = pair.slice(equalityPosition + 1); queryParams[key] = value; } return queryParams; } - +//console.log(parseQueryString(["amir = male"])); module.exports = parseQueryString; diff --git a/Sprint-2/implement/querystring.test.js b/Sprint-2/implement/querystring.test.js index 3e218b789..5e6f66def 100644 --- a/Sprint-2/implement/querystring.test.js +++ b/Sprint-2/implement/querystring.test.js @@ -3,10 +3,22 @@ // 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 =", () => { +test("the function should return object when a string parses querystring values containing = is passed", () => { expect(parseQueryString("equation=x=y+1")).toEqual({ - "equation": "x=y+1", + equation: "x=y+1", + }); + expect(parseQueryString("equation")).toEqual({}); + expect(parseQueryString("name=ahmed&age=29")).toEqual({ + name: "ahmed", + age: "29", + }); + expect(parseQueryString("q=sa3")).toEqual({ q: "sa3" }); + expect(parseQueryString("code=future+codebetter")).toEqual({ + code: "future+codebetter", + }); + expect(parseQueryString("location=london=x=+12=2")).toEqual({ + location: "london=x=+12=2", }); }); diff --git a/Sprint-2/implement/tally.js b/Sprint-2/implement/tally.js index f47321812..eae175880 100644 --- a/Sprint-2/implement/tally.js +++ b/Sprint-2/implement/tally.js @@ -1,3 +1,18 @@ -function tally() {} +function tally(myArray) { + if (myArray.length === 0) { + return {}; + } else if (typeof myArray === "string") { + throw Error; + } + let tallyObject = {}; + for (const singleItems of myArray) { + if (tallyObject[singleItems] === undefined) { + tallyObject[singleItems] = 1; + } else { + tallyObject[singleItems] += 1; + } + } + return tallyObject; +} module.exports = tally; diff --git a/Sprint-2/implement/tally.test.js b/Sprint-2/implement/tally.test.js index 2ceffa8dd..f702e4ee1 100644 --- a/Sprint-2/implement/tally.test.js +++ b/Sprint-2/implement/tally.test.js @@ -23,12 +23,31 @@ 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("The function should return empty object when an empty array is passed to the function", () => { + expect(tally([])).toEqual({}); +}); // Given an array with duplicate items // When passed to tally // Then it should return counts for each unique item - +test("The function should return an object where each key is a unique item from the array, and each index is how many times that item duplicate", () => { + expect(tally(["a", "a", "a", "b", "b", "b", "c", "c", "c"])).toEqual({ + a: 3, + b: 3, + c: 3, + }); + expect( + tally(["manchester", "manchester", "london", "london", "leeds"]) + ).toEqual({ manchester: 2, london: 2, leeds: 1 }); + expect(tally([1, 1, 2])).toEqual({ 1: 2, 2: 1 }); + expect(tally(["a"])).toEqual({ a: 1 }); + expect(tally(["a", "a", "a"])).toEqual({ a: 3 }); +}); // Given an invalid input like a string // When passed to tally // Then it should throw an error +test("The function should throw an error when invalid input is passed", () => { + expect(tally("hello")).toThrow; + expect(tally(134)).toThrow; + expect(tally({ a: 1, b: 3, d: 2 })).toThrow; +}); diff --git a/Sprint-2/interpret/invert.js b/Sprint-2/interpret/invert.js index bb353fb1f..0efb84c7e 100644 --- a/Sprint-2/interpret/invert.js +++ b/Sprint-2/interpret/invert.js @@ -17,13 +17,29 @@ function invert(obj) { } // a) What is the current return value when invert is called with { a : 1 } +// the current return value when invert is called with { a:1 } is { key: 1 } // b) What is the current return value when invert is called with { a: 1, b: 2 } +// the current return value when invert is called with { a: 1, b: 2 } is { key: 2} // c) What is the target return value when invert is called with {a : 1, b: 2} +// the target return value when invert is called with {a : 1, b: 2} is { '1':'a','2':'b'} // c) What does Object.entries return? Why is it needed in this program? +// Object.entries is needed because it will gave as the access of the key and value by separating each key value pair. // d) Explain why the current return value is different from the target output +/*the current value is different from the targeted value because the for loop already give as access for the key value pairs +but when we declare a new object the function where looking the word key in the loop because it was dot notation and taking the +word key and pairing it withe the last loop value. */ // 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[value] = key; + } + + return invertedObj; +} */