diff --git a/Sprint-2/debug/address.js b/Sprint-2/debug/address.js index 940a6af8..ffb1f485 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... - I don't see the return, I think it's a problem. I would change [0] to (houseNumber), it's will show undefined error // This code should log out the houseNumber from the address object // but it isn't working... @@ -10,6 +10,14 @@ const address = { city: "Manchester", country: "England", postcode: "XYZ 123", + }; -console.log(`My house number is ${address[0]}`); +console.log(`My house number is ${address[0]}`); // - address is an object, not an array. So JavaScript doesn't know what address[0] is — it'll return undefined. +console.log(`My house number is ${address["houseNumber"]}`); + +console.log(`My city is ${address["city"]}`); +//or +console.log(`My city is ${address.city}`); + +//I had mistake in my prediction, it does't need return because it's not a function. diff --git a/Sprint-2/debug/author.js b/Sprint-2/debug/author.js index 8c212597..6c863256 100644 --- a/Sprint-2/debug/author.js +++ b/Sprint-2/debug/author.js @@ -1,7 +1,7 @@ -// Predict and explain first... +// Predict and explain first... It's will be undefined. I will create separate const value and just after made console.log (value). // This program attempts to log out all the property values in the object. -// But it isn't working. Explain why first and then fix the problem +// But it isn't working. Explain why first and then fix the problem - it's showing typeError. I think it's because 'for' as I know it's for loop in arrays but I don't see any loop there. const author = { firstName: "Zadie", @@ -11,6 +11,15 @@ const author = { alive: true, }; -for (const value of author) { +// for (const value of author) { +// console.log(value); +// } + +for (const value of Object.values(author)) { console.log(value); } + +// const value = author +// console.log(value); + + diff --git a/Sprint-2/debug/recipe.js b/Sprint-2/debug/recipe.js index 6cbdd22c..87b571d0 100644 --- a/Sprint-2/debug/recipe.js +++ b/Sprint-2/debug/recipe.js @@ -1,8 +1,8 @@ -// Predict and explain first... +// Predict and explain first...- I think it's will find 'title' and 'serves' but will not find 'ingredients', ingredients will be undefined. // This program should log out the title, how many it serves and the ingredients. -// Each ingredient should be logged on a new line -// How can you fix it? +// Each ingredient should be logged on a new line +// How can you fix it? - I will write console.log for each ingredient's separate , so it's will be in the new line const recipe = { title: "bruschetta", @@ -10,6 +10,33 @@ const recipe = { ingredients: ["olive oil", "tomatoes", "salt", "pepper"], }; +// console.log(`${recipe.title} serves ${recipe.serves} +// ingredients: +// ${recipe}`); + +//// + console.log(`${recipe.title} serves ${recipe.serves} - ingredients: -${recipe}`); +ingredients: + ${recipe.ingredients[0]} + ${recipe.ingredients[1]} + ${recipe.ingredients[2]} + ${recipe.ingredients[3] + }`); + + //or + + console.log(recipe.title); +console.log(`Serves: ${recipe.serves}`); +console.log("Ingredients:"); +for (const ingredient of recipe.ingredients) { + console.log(ingredient); +} + +//or + +console.log(`${recipe.title} serves ${recipe.serves}`); +console.log(recipe.ingredients[0]); +console.log(recipe.ingredients[1]); +console.log(recipe.ingredients[2]); +console.log(recipe.ingredients[3]); \ No newline at end of file diff --git a/Sprint-2/implement/contains.js b/Sprint-2/implement/contains.js index cd779308..15689eff 100644 --- a/Sprint-2/implement/contains.js +++ b/Sprint-2/implement/contains.js @@ -1,3 +1,18 @@ -function contains() {} +function contains(obj, property) { + + if (typeof obj !== "object" || obj === null || Array.isArray(obj)) { + return false; + } + + return property in obj; + } + +console.log(contains({ a: 1, b: 2 }, "a")); // true +console.log(contains({ a: 1, b: 2 }, "c")); // false +console.log(contains({}, "a")); // false +console.log(contains(null, "a")); // false +console.log(contains([], "length")); // false +console.log(contains(42, "a")); // false +console.log(contains("string", "length")); // false module.exports = contains; diff --git a/Sprint-2/implement/contains.test.js b/Sprint-2/implement/contains.test.js index 326bdb1f..a02f7d67 100644 --- a/Sprint-2/implement/contains.test.js +++ b/Sprint-2/implement/contains.test.js @@ -33,3 +33,27 @@ test.todo("contains on empty object returns false"); // Given invalid parameters like an array // When passed to contains // Then it should return false or throw an error + + +describe("contains function", () => { + + // Test 1: Given an empty object, should return false + test("contains on empty object returns false", () => { + expect(contains({}, 'a')).toBe(false); + }); + + // Test 2: Given an object with properties, should return true for an existing property + test("contains with existing property returns true", () => { + expect(contains({ a: 1, b: 2 }, 'a')).toBe(true); + expect(contains({ a: 1, b: 2 }, 'b')).toBe(true); + }); + + // Test 3: Given an object with properties, should return false for a non-existent property + test("contains with non-existent property returns false", () => { + expect(contains({ a: 1, b: 2 }, 'c')).toBe(false); + expect(contains({ a: 1, b: 2 }, 'z')).toBe(false); + }); + + + + }); diff --git a/Sprint-2/implement/lookup.js b/Sprint-2/implement/lookup.js index a6746e07..20f5d668 100644 --- a/Sprint-2/implement/lookup.js +++ b/Sprint-2/implement/lookup.js @@ -1,5 +1,18 @@ -function createLookup() { - // implementation here + + +function createLookup(array) { + let object = {} + for (const list of array) { + console.log(list); + object[list[0]] = list[1] + } + console.log (object) + return object + } +//const countryCurrencyPairs = [['US', 'USD'], ['CA', 'CAD'], ['UA', 'GRN']] +//createLookup(countryCurrencyPairs) + + module.exports = createLookup; diff --git a/Sprint-2/implement/lookup.test.js b/Sprint-2/implement/lookup.test.js index 547e06c5..daa522ef 100644 --- a/Sprint-2/implement/lookup.test.js +++ b/Sprint-2/implement/lookup.test.js @@ -32,4 +32,16 @@ It should return: 'US': 'USD', 'CA': 'CAD' } -*/ +*/ +//const countryCurrencyPairs = [['US', 'USD'], ['CA', 'CAD']] +//createLookup(countryCurrencyPairs) + +describe("lookup", () => { + test("should return object in muly courency", () => { + expect(createLookup([['US', 'USD'], ['CA', 'CAD']])).toEqual({ + 'US': 'USD', + 'CA': 'CAD' + }); + }); +}) + diff --git a/Sprint-2/implement/querystring.js b/Sprint-2/implement/querystring.js index 45ec4e5f..26dba25e 100644 --- a/Sprint-2/implement/querystring.js +++ b/Sprint-2/implement/querystring.js @@ -6,11 +6,19 @@ function parseQueryString(queryString) { const keyValuePairs = queryString.split("&"); for (const pair of keyValuePairs) { - const [key, value] = pair.split("="); + const [key, value] = pair.split(/=(.*)/); queryParams[key] = value; } return queryParams; } +parseQueryString("equation=x=y+1") + +// Object { +// - "equation": "x=y+1", - ожидаем +// + "equation": "x", - то что мы получили + +//"equation=x=y+1" - передали +// equation": "x=y+1 - ожидаем получить module.exports = parseQueryString; diff --git a/Sprint-2/implement/querystring.test.js b/Sprint-2/implement/querystring.test.js index 3e218b78..eb9b6a5f 100644 --- a/Sprint-2/implement/querystring.test.js +++ b/Sprint-2/implement/querystring.test.js @@ -10,3 +10,4 @@ test("parses querystring values containing =", () => { "equation": "x=y+1", }); }); + diff --git a/Sprint-2/implement/tally.js b/Sprint-2/implement/tally.js index f4732181..5387e074 100644 --- a/Sprint-2/implement/tally.js +++ b/Sprint-2/implement/tally.js @@ -1,3 +1,25 @@ -function tally() {} + function tally(items) { + + if (!Array.isArray(items)) { + throw new Error("Input must be an array"); + } + + const counts = {}; + items.forEach(element => { + if (counts[element]) { + counts[element]++; + } else { + counts[element] = 1; + } + }); + return counts; + } + + console.log(tally(['a'])); // { a: 1 } + console.log(tally(['a', 'a', 'a'])); // { a: 3 } + console.log(tally(['a', 'a', 'b', 'c'])); // { a: 2, b: 1, c: 1 } + console.log(tally([])); // {} + + module.exports = tally; diff --git a/Sprint-2/interpret/invert.js b/Sprint-2/interpret/invert.js index bb353fb1..a6f50723 100644 --- a/Sprint-2/interpret/invert.js +++ b/Sprint-2/interpret/invert.js @@ -10,20 +10,26 @@ function invert(obj) { const invertedObj = {}; for (const [key, value] of Object.entries(obj)) { - invertedObj.key = value; + invertedObj [value] = key; } - return invertedObj; } -// 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 } +console.log(invert({ a: 1 })); // Expected: { "1": "a" } // b) What is the current return value when invert is called with { a: 1, b: 2 } - +console.log(invert({ a: 1, b: 2 })); // Expected: { "1": "a", "2": "b" } // c) What is the target return value when invert is called with {a : 1, b: 2} -// c) What does Object.entries return? Why is it needed in this program? +// the same with b) // d) Explain why the current return value is different from the target output - +console.log(invert({ x: 10, y: 20 })); // Expected: { "10": "x", "20": "y" } // e) Fix the implementation of invert (and write tests to prove it's fixed!) + + +console.log(invert({ a: 1 })); // Expected: { "1": "a" } +console.log(invert({ a: 1, b: 2 })); // Expected: { "1": "a", "2": "b" } +console.log(invert({ x: 10, y: 20 })); // Expected: { "10": "x", "20": "y" } +console.log(invert({}));