Skip to content

West Midlands | ITP-MAY25 | Saleh Yousef | Module-Data-Groups | Sprint-2 #622

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
4 changes: 3 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.

Correct

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
// but it isn't working...
// Fix anything that isn't working

// I Think the issue is the console.log statement. I will fix it buy using dot notation to access the houseNumber property of the address object.
// It is trying to access the houseNumber using an index, but it should use the property houseNumber.
const address = {
houseNumber: 42,
street: "Imaginary Road",
Expand All @@ -12,4 +14,4 @@ const address = {
postcode: "XYZ 123",
};

console.log(`My house number is ${address[0]}`);
console.log(`My house number is ${address.houseNumber}`);
4 changes: 2 additions & 2 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.

Correct

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Predict and explain first...
// Predict and explain first... It won't work because the method `for...of` is used for iterable objects like arrays.

// 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
Expand All @@ -11,6 +11,6 @@ const author = {
alive: true,
};

for (const value of author) {
for (const value of Object.values(author)) {
console.log(value);
}
8 changes: 5 additions & 3 deletions Sprint-2/debug/recipe.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Predict and explain first...
// Predict and explain first... it won't work because the ingredients are not being accessed correctly.

// This program should log out the title, how many it serves and the ingredients.
// Each ingredient should be logged on a new line
Expand All @@ -11,5 +11,7 @@ const recipe = {
};

console.log(`${recipe.title} serves ${recipe.serves}
ingredients:
${recipe}`);
ingredients:`);
Copy link

Choose a reason for hiding this comment

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

Minor query here shouldn't the console read out "bruschetta serves 2, the ingredients are: ". Your code reads "bruschetta serves 2 ingredients: ". Otherwise this works

recipe.ingredients.forEach((ingredient) => {
console.log(ingredient);
});
8 changes: 7 additions & 1 deletion Sprint-2/implement/contains.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
function contains() {}
function contains(obj, key) {
if (typeof obj !== "object" || obj === null || Array.isArray(obj)) {
return false;
}
return Object.prototype.hasOwnProperty.call(obj, key);

}

module.exports = contains;
40 changes: 39 additions & 1 deletion Sprint-2/implement/contains.test.js
Copy link

Choose a reason for hiding this comment

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

tests passed

Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,58 @@ as the object doesn't contains a key of 'c'
// Given a contains function
// When passed an object and a property name
// Then it should return true if the object contains the property, false otherwise
test("contains returns true for existing property", () => {
const currentOutput = contains({ k: 13, b: 5 }, 'k');
const targetOutput = true;

expect(currentOutput).toEqual(targetOutput);
});
test("contains returns false for non-existent property", () => {
const currentOutput = contains({ k: 13, b: 5 }, 'x');
const targetOutput = false;

expect(currentOutput).toEqual(targetOutput);
});

// 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 currentOutput = contains({}, 'anyProperty');
const targetOutput = false;
expect(currentOutput).toEqual(targetOutput);
});

// Given an object with properties
// When passed to contains with an existing property name
// Then it should return true
test("contains returns true for existing property in object", () => {
const currentOutput = contains({ name: "Alice", age: 30 }, 'name');
const targetOutput = true;
expect(currentOutput).toEqual(targetOutput);
});

// Given an object with properties
// When passed to contains with a non-existent property name
// Then it should return false
test("contains returns false for non-existent property in object", () => {
const currentOutput = contains({ name: "Alice", age: 30 }, 'address');
const targetOutput = false;
expect(currentOutput).toEqual(targetOutput);
});

// Given invalid parameters like an array
// When passed to contains
// Then it should return false or throw an error
test("contains returns false for array input", () => {
const currentOutput = contains([1, 2, 3], 'length');
const targetOutput = false; // Arrays are objects, but 'length' is not a property in this context
expect(currentOutput).toEqual(targetOutput);
});

// Given an object with nested properties
test("contains returns the correct value for nested properties", () => {
const currentOutput = contains ({ person: { name: "Bob", age: 25 } }, 'person');
const targetOutput = true;
expect(currentOutput).toEqual(targetOutput);
});
10 changes: 8 additions & 2 deletions Sprint-2/implement/lookup.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
function createLookup() {
// implementation here
function createLookup(pairs) {
const result = {};

for (const [countryCode, currencyCode] of pairs) {
result[countryCode] = currencyCode;
}

return result;
}

module.exports = createLookup;
23 changes: 21 additions & 2 deletions Sprint-2/implement/lookup.test.js
Copy link

Choose a reason for hiding this comment

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

Tests passed

Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
const createLookup = require("./lookup.js");

test.todo("creates a country currency code lookup for multiple codes");

/*
Create a lookup object of key value pairs from an array of code pairs
Expand All @@ -23,6 +21,7 @@ Then
Example
Given: [['US', 'USD'], ['CA', 'CAD']]
When
createLookup(countryCurrencyPairs) is called
Expand All @@ -33,3 +32,23 @@ It should return:
'CA': 'CAD'
}
*/



test('returns correct lookup object for valid input', () => {
const input = [['US', 'USD'], ['CA', 'CAD']];
const expected = { US: 'USD', CA: 'CAD' };
expect(createLookup(input)).toEqual(expected);
});

test('handles mixed data correctly', () => {
const input = [['JP', 'JPY'], ['FR', 'EUR'], ['GB', 'GBP']];
const expected = { JP: 'JPY', FR: 'EUR', GB: 'GBP' };
expect(createLookup(input)).toEqual(expected);
});

test('returns empty object for empty input', () => {
const input = [];
const expected = {};
expect(createLookup(input)).toEqual(expected);
});
4 changes: 2 additions & 2 deletions Sprint-2/implement/querystring.js
Copy link

Choose a reason for hiding this comment

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

code works however do some further research on how this could be improved. For example dealing with URL encoded string(decodeURIComponent). Then you could write a test to see if your solution works.

Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ function parseQueryString(queryString) {
const keyValuePairs = queryString.split("&");

for (const pair of keyValuePairs) {
const [key, value] = pair.split("=");
queryParams[key] = value;
const [key, ...rest] = pair.split("=");
queryParams[key] = rest.join("=");
Comment on lines +9 to +10
Copy link
Member

Choose a reason for hiding this comment

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

It looks like you fixed a bug here but didn't write a test for it - can you add a test for the edge case you fixed?

}

return queryParams;
Expand Down
18 changes: 18 additions & 0 deletions Sprint-2/implement/querystring.test.js
Copy link

Choose a reason for hiding this comment

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

test passed

Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,21 @@ test("parses querystring values containing =", () => {
"equation": "x=y+1",
});
});

test("parses querystring with multiple values", () => {
expect(parseQueryString("name=John&age=30")).toEqual({
"name": "John",
"age": "30",
});
});
test("parses querystring with multiple values with same key", () => {
expect(parseQueryString("name=John&name=Jane")).toEqual({
"name": "Jane", // Last value should overwrite previous ones
});
});

test("parses querystring without key", () => {
Comment on lines +13 to +26
Copy link
Member

Choose a reason for hiding this comment

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

Good edge cases!

expect(parseQueryString("=Python")).toEqual({
"": "Python", // Empty key should be handled
});
});
17 changes: 16 additions & 1 deletion Sprint-2/implement/tally.js
Copy link

Choose a reason for hiding this comment

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

Code works for tests. Do some further research on how code could be improved to handle edge cases such as undefined values.

Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
function tally() {}
function tally(array) {
if (!Array.isArray(array)) {
throw new Error("Input must be an array");
}
const counts = {};

for (const item of array) {
if (counts[item]) {
counts[item] += 1;
} else {
counts[item] = 1;
}
}

return counts;
}

module.exports = tally;
14 changes: 13 additions & 1 deletion Sprint-2/implement/tally.test.js
Copy link

Choose a reason for hiding this comment

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

tests passed

Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,24 @@ 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", () => {
const currentOutput = tally([]);
const targetOutput = {};
expect(currentOutput).toEqual(targetOutput);
});

// Given an array with duplicate items
// When passed to tally
// Then it should return counts for each unique item
test("tally counts unique items in an array", () => {
const currentOutput = tally(['a', 'b', 'a', 'c', 'b']);
const targetOutput = { a: 2, b: 2, c: 1 };
expect(currentOutput).toEqual(targetOutput);
});

// Given an invalid input like a string
// When passed to tally
// Then it should throw an error
test("tally throws an error for invalid input", () => {
expect(() => tally("invalid input")).toThrow("Input must be an array");
});
28 changes: 21 additions & 7 deletions Sprint-2/interpret/invert.js
Copy link

Choose a reason for hiding this comment

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

Both tests pass - well done

Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,34 @@ function invert(obj) {
const invertedObj = {};

for (const [key, value] of Object.entries(obj)) {
invertedObj.key = value;
invertedObj[value] = key;
}

return invertedObj;
return invertedObj;
}

// a) What is the current return value when invert is called with { a : 1 }

{1 : a}
// b) What is the current return value when invert is called with { a: 1, b: 2 }

{1: a, 2: b}
// 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?

// Object.entries returns an array of key-value pairs from the object. It is needed to iterate over both keys and values in the object.
// d) Explain why the current return value is different from the target output

// The current return value is incorrect because the code is assigning the value to the key instead of the key to the value in the inverted object.
// e) Fix the implementation of invert (and write tests to prove it's fixed!)

test("invert swaps keys and values in an object", () => {
const currentOutput = invert({ x: 10, y: 20 });
const targetOutput = { "10": "x", "20": "y" };

expect(currentOutput).toEqual(targetOutput);
});

test("invert handles multiple key-value pairs", () => {
const currentOutput = invert({ a: 1, b: 2, c: 3 });
const targetOutput = { "1": "a", "2": "b", "3": "c" };

expect(currentOutput).toEqual(targetOutput);
});
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.