Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
53fa90f
implement correct median calculation
Mar 7, 2026
7b40348
Access houseNumber correctly from address object
Mar 26, 2026
c04e2dc
Use Object.values to loop through author object
Mar 26, 2026
59fff2a
display ingredients correctly instead of object
Mar 26, 2026
bbdc491
Add contains function to check if value exists in array
Mar 26, 2026
904055c
implement contains function for object property check
Mar 26, 2026
7f28769
implement contains test cases
Mar 26, 2026
e6ec9d4
Made it look clean
Mar 26, 2026
067f3e1
Implement createLookup function for country-currency pairs
Mar 26, 2026
5b88737
set up jest and add test script
Mar 26, 2026
92c0f04
1. add createLookup test cases for multiple and empty inputs
Mar 26, 2026
12f79f0
Correctly parse query strings with '=' in values and edge cases"
Mar 26, 2026
3c7d554
Implement tally function with error handling and tests"
Mar 26, 2026
78d5e44
Correctly invert object keys and values using bracket notation"
Mar 26, 2026
4c3daf6
Revert to main
Mar 27, 2026
db6200c
Merge branch 'main' into sprint-2
Pretty548 Mar 27, 2026
c3b7bf0
Merge branch 'sprint-2' of https://github.com/Pretty548/Module-Data-G…
Mar 27, 2026
1b5a7e8
removed the package.json
Mar 27, 2026
1f43925
Fix contains to return false for non-object inputs
Mar 28, 2026
84d5a4e
Use Object.hasOwn and handle non-object inputs correctly
Mar 28, 2026
b996f9f
Improve array test to ensure contains returns false for valid array keys
Mar 28, 2026
92779c2
Handle empty query string pairs correctly
Mar 28, 2026
c864665
Add invert.test.js and move tests from invert.js
Mar 28, 2026
c547fdc
Separate invert tests from implementation into invert.test.js
Mar 28, 2026
cdfdd7c
Co-authored-by: Isaac Abodunrin <bytesandroses@users.noreply.github.c…
Mar 28, 2026
7846797
deleted an unnecessary file
Mar 28, 2026
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
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ const address = {
postcode: "XYZ 123",
};

console.log(`My house number is ${address[0]}`);
console.log(`My house number is ${address.houseNumber}`);
2 changes: 1 addition & 1 deletion Sprint-2/debug/author.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ const author = {
alive: true,
};

for (const value of author) {
for (const value of Object.values(author)) {
console.log(value);
}
2 changes: 1 addition & 1 deletion Sprint-2/debug/recipe.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ const recipe = {

console.log(`${recipe.title} serves ${recipe.serves}
ingredients:
${recipe}`);
${recipe.ingredients.join("\n")}`);
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.hasOwn(obj, key);
}

module.exports = contains;
39 changes: 20 additions & 19 deletions Sprint-2/implement/contains.test.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,36 @@
const contains = require("./contains.js");

/*
Implement a function called contains that checks an object contains a
particular property
E.g. contains({a: 1, b: 2}, 'a') // returns true
as the object contains a key of 'a'
E.g. contains({a: 1, b: 2}, 'c') // returns false
as the object doesn't contains a key of 'c'
*/

// Acceptance criteria:

// 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

// Given an empty object
// When passed to contains
// When passed to contains with any key
// Then it should return false
test.todo("contains on empty object returns false");
test("returns false for an empty object", () => {
expect(contains({}, "a")).toBe(false);
});

// Given an object with properties
// When passed to contains with an existing property name
// Then it should return true
test("returns true for an existing property", () => {
expect(contains({ a: 1, b: 2 }, "a")).toBe(true);
});

// Given an object with properties
// When passed to contains with a non-existent property name
// Then it should return false
test("returns false for a non-existent property", () => {
expect(contains({ a: 1, b: 2 }, "c")).toBe(false);
});

// Given invalid parameters like an array
// When passed to contains
// Then it should return false or throw an error
test("returns false for an array", () => {
expect(contains([1, 2, 3], 0)).toBe(false);
});

Comment on lines +27 to +30
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This test does not yet confirm that the function correctly returns false when the first argument is an array.
This is because contains([], "a") could also return false simply because "a" is not a key of the array.

Arrays are objects, with their indices acting as keys. A proper test should use a non-empty array along with a valid
key to ensure the function returns false specifically because the input is an array, not because the key is missing.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Good point! I’ve updated the test to use a non-empty array with a valid index as the key, so it confirms the function returns false specifically because the input is an array.

// Given invalid parameters like null
// When passed to contains
// Then it should return false or throw an error
test("returns false for null", () => {
expect(contains(null, "a")).toBe(false);
});
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 (let i = 0; i < pairs.length; i++) {
const [country, currency] = pairs[i];

result[country] = currency;
}
return result;
}

module.exports = createLookup;
54 changes: 21 additions & 33 deletions Sprint-2/implement/lookup.test.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,23 @@
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

Acceptance Criteria:

Given
- An array of arrays representing country code and currency code pairs
e.g. [['US', 'USD'], ['CA', 'CAD']]

When
- createLookup function is called with the country-currency array as an argument

Then
- It should return an object where:
- The keys are the country codes
- The values are the corresponding currency codes

Example
Given: [['US', 'USD'], ['CA', 'CAD']]

When
createLookup(countryCurrencyPairs) is called

Then
It should return:
{
'US': 'USD',
'CA': 'CAD'
}
*/
test("creates a country currency code lookup for multiple codes", () => {
const input = [
["US", "USD"],
["CA", "CAD"],
["GB", "GBP"],
];
const expected = {
US: "USD",
CA: "CAD",
GB: "GBP",
};

expect(createLookup(input)).toEqual(expected);
});

test("creates a country currency code lookup for an empty array", () => {
const input = [];
const expected = {};

expect(createLookup(input)).toEqual(expected);
});
28 changes: 18 additions & 10 deletions Sprint-2/implement/querystring.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
function parseQueryString(queryString) {
const queryParams = {};
if (queryString.length === 0) {
return queryParams;
}
const keyValuePairs = queryString.split("&");
const result = {};
if (!queryString) return result;

for (const pair of keyValuePairs) {
const [key, value] = pair.split("=");
queryParams[key] = value;
}
const pairs = queryString.split("&");

return queryParams;
pairs.forEach((pair) => {
if (pair === "") return;

const index = pair.indexOf("=");

if (index === -1) {
result[pair] = "";
} else {
const key = pair.slice(0, index);
const value = pair.slice(index + 1);
result[key] = value;
}
});
Comment on lines +7 to +19
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

For the following function call, does your function return the value you expect?

parseQueryString("key1=value1&&key2=value2")

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Yes, the function correctly handles this case. The double "&&" creates an empty string when splitting, and the check if (!pair) return; ensures it is ignored. The function returns the expected result with both key-value pairs.


return result;
}

module.exports = parseQueryString;
44 changes: 42 additions & 2 deletions Sprint-2/implement/querystring.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,50 @@
// 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 =", () => {
expect(parseQueryString("equation=x=y+1")).toEqual({
"equation": "x=y+1",
equation: "x=y+1",
});
});

test("parses multiple key-value pairs", () => {
expect(parseQueryString("name=Alice&age=30&city=NY")).toEqual({
name: "Alice",
age: "30",
city: "NY",
});
});

test("returns an empty object for an empty query string", () => {
expect(parseQueryString("")).toEqual({});
});

test("handles key without equals sign", () => {
expect(parseQueryString("key1=value1&key2&key3=value3")).toEqual({
key1: "value1",
key2: "",
key3: "value3",
});
});

test("handles multiple '=' in value", () => {
expect(parseQueryString("data=a=b=c")).toEqual({
data: "a=b=c",
});
});

test("handles empty key", () => {
expect(parseQueryString("=value")).toEqual({
"": "value",
});
});

test("handles mixed querystring", () => {
expect(parseQueryString("a=1&b&c=3=d")).toEqual({
a: "1",
b: "",
c: "3=d",
});
});
18 changes: 17 additions & 1 deletion Sprint-2/implement/tally.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
function tally() {}
function tally(items) {
if (!Array.isArray(items)) {
throw new Error("Input must be an array");
}

const result = {};

items.forEach((item) => {
if (result[item]) {
result[item]++;
} else {
result[item] = 1;
}
});
Comment on lines +6 to +14
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Does the following function call returns the value you expect?

tally(["toString", "toString"]);

Suggestion: Look up an approach to create an empty object with no inherited properties.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Good catch! I updated the implementation to use Object.create(null) so the result object has no inherited properties. This ensures keys like "toString" are handled correctly.


return result;
}

module.exports = tally;
10 changes: 9 additions & 1 deletion Sprint-2/implement/tally.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,20 @@ 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", () => {
expect(tally([])).toEqual({});
});

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

// Given an invalid input like a string
// When passed to tally
// Then it should throw an error
test("tally on an invalid input throws an error", () => {
expect(() => tally("not an array")).toThrow("Input must be an array");
});
3 changes: 2 additions & 1 deletion Sprint-2/interpret/invert.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ function invert(obj) {
const invertedObj = {};

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

return invertedObj;
}
module.exports = invert;

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

Expand Down
24 changes: 24 additions & 0 deletions Sprint-2/interpret/invert.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const invert = require("./invert");

test("inverts a simple object", () => {
expect(invert({ a: 1 })).toEqual({ 1: "a" });
});

test("inverts an object with multiple key-value pairs", () => {
expect(invert({ a: 1, b: 2 })).toEqual({
1: "a",
2: "b",
});
});

test("returns empty object when given an empty object", () => {
expect(invert({})).toEqual({});
});

test("handles non-string values as keys in the inverted object", () => {
expect(invert({ a: 1, b: true, c: null })).toEqual({
1: "a",
true: "b",
null: "c",
});
});
Loading