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 1 commit
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
2 changes: 1 addition & 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,7 +4,7 @@
// but it isn't working...
// Fix anything that isn't working

// I Think the issue isin the console.log statement. i will fix it buy using dot notation to access the houseNumber property of the address object.
// 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,
Expand Down
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");
});
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.