Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ITP-JAN-25 | LONDON | Maryna Romanova | Module-Data-Groups | Sprint 2 #451

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion Sprint-2/debug/address.js
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

House number is not defined in const address. Also investigate the console.log method. Suppose I want to return the city. How would I amend console.log to make sure it prints this out.

Original file line number Diff line number Diff line change
@@ -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...
Expand All @@ -10,6 +10,10 @@ const address = {
city: "Manchester",
country: "England",
postcode: "XYZ 123",

};

console.log(`My house number is ${address[0]}`);
console.log(`My house number is ${address["houseNumber"]}`);

//I had mistake in my prediction, it does't need return because it's not a function.
11 changes: 8 additions & 3 deletions Sprint-2/debug/author.js
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error message says this object is not iterable. Have a look at this page to assist with solution-> https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols

Original file line number Diff line number Diff line change
@@ -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 but I don't see any loop there.

const author = {
firstName: "Zadie",
Expand All @@ -12,5 +12,10 @@ const author = {
};

for (const value of author) {
console.log(value);
console.log(value);
}

const value = author
console.log(value);


14 changes: 11 additions & 3 deletions Sprint-2/debug/recipe.js
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't there be properties for "serves" and "ingredients" to complete this code?

Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -13,3 +13,11 @@ const recipe = {
console.log(`${recipe.title} serves ${recipe.serves}
ingredients:
${recipe}`);

////

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]);
17 changes: 16 additions & 1 deletion Sprint-2/implement/contains.js
Original file line number Diff line number Diff line change
@@ -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;
24 changes: 24 additions & 0 deletions Sprint-2/implement/contains.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});



});
17 changes: 15 additions & 2 deletions Sprint-2/implement/lookup.js
Original file line number Diff line number Diff line change
@@ -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;
14 changes: 13 additions & 1 deletion Sprint-2/implement/lookup.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
});
});
})

10 changes: 9 additions & 1 deletion Sprint-2/implement/querystring.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
1 change: 1 addition & 0 deletions Sprint-2/implement/querystring.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ test("parses querystring values containing =", () => {
"equation": "x=y+1",
});
});

24 changes: 23 additions & 1 deletion Sprint-2/implement/tally.js
Original file line number Diff line number Diff line change
@@ -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;
14 changes: 10 additions & 4 deletions Sprint-2/interpret/invert.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,19 @@ function invert(obj) {
}

// 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?
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

c) and d) answers to be expanded

// 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({}));