Skip to content

Commit 38a07a2

Browse files
Sprint 2 - Data group
1 parent 39afe22 commit 38a07a2

File tree

15 files changed

+109
-3785
lines changed

15 files changed

+109
-3785
lines changed

Sprint-2/debug/address.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@ const address = {
1212
postcode: "XYZ 123",
1313
};
1414

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

Sprint-2/debug/author.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ const author = {
1111
alive: true,
1212
};
1313

14-
for (const value of author) {
14+
for (const value of Object.values(author)) {
1515
console.log(value);
1616
}

Sprint-2/debug/recipe.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,8 @@ const recipe = {
1111
};
1212

1313
console.log(`${recipe.title} serves ${recipe.serves}
14-
ingredients:
15-
${recipe}`);
14+
ingredients:`);
15+
16+
for (const ingredient of recipe.ingredients) {
17+
console.log(ingredient);
18+
};

Sprint-2/implement/contains.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1-
function contains() {}
1+
function contains(obj, prop) {
2+
if (typeof obj !== 'object' || obj === null || Array.isArray(obj)) return false;
3+
return Object.prototype.hasOwnProperty.call(obj, prop);
4+
}
25

36
module.exports = contains;

Sprint-2/implement/contains.test.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,27 @@ as the object doesn't contains a key of 'c'
2020
// Given an empty object
2121
// When passed to contains
2222
// Then it should return false
23-
test.todo("contains on empty object returns false");
23+
test("contains on empty object returns false", () => {
24+
expect(contains({}, "a")).toBe(false);
25+
});
2426

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

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

3341
// Given invalid parameters like an array
3442
// When passed to contains
3543
// Then it should return false or throw an error
44+
test("contains returns false for invalid parameters", () => {
45+
expect(contains(3, "a")).toBe(false);
46+
});

Sprint-2/implement/lookup.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
1-
function createLookup() {
2-
// implementation here
1+
function createLookup(currencyPairs) {
2+
const lookup = {};
3+
if (!Array.isArray(currencyPairs)) return lookup;
4+
5+
for (const pair of currencyPairs) {
6+
if (Array.isArray(pair) && pair.length === 2) {
7+
const [countryCode, currencyCode] = pair;
8+
lookup[countryCode] = currencyCode;
9+
}
10+
}
11+
12+
return lookup;
313
}
414

515
module.exports = createLookup;

Sprint-2/implement/lookup.test.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
const createLookup = require("./lookup.js");
22

3-
test.todo("creates a country currency code lookup for multiple codes");
4-
3+
test("creates a country currency code lookup for multiple codes", () => {
4+
const input = [['US', 'USD'], ['CA', 'CAD']];
5+
const expected = {
6+
US: 'USD',
7+
CA: 'CAD',
8+
};
9+
10+
expect(createLookup(input)).toEqual(expected);
11+
});
512
/*
613
714
Create a lookup object of key value pairs from an array of code pairs

Sprint-2/implement/querystring.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ function parseQueryString(queryString) {
66
const keyValuePairs = queryString.split("&");
77

88
for (const pair of keyValuePairs) {
9-
const [key, value] = pair.split("=");
10-
queryParams[key] = value;
9+
const [key, ...rest] = pair.split("=");
10+
const value = rest.join("=");
11+
queryParams[decodeURIComponent(key)] = decodeURIComponent(value);
12+
1113
}
1214

1315
return queryParams;

Sprint-2/implement/querystring.test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,14 @@ test("parses querystring values containing =", () => {
1010
"equation": "x=y+1",
1111
});
1212
});
13+
14+
test("parses empty querystring", () => {
15+
expect(parseQueryString("")).toEqual({});
16+
});
17+
18+
test("parses querystring with multiple parameters", () => {
19+
expect(parseQueryString("name=Nuel&age=75")).toEqual({
20+
"name": "Nuel",
21+
"age": "75",
22+
});
23+
});

Sprint-2/implement/tally.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1-
function tally() {}
1+
function tally(items) {
2+
3+
if (!Array.isArray(items)) {
4+
throw new Error("Input must be an array");
5+
}
6+
7+
const counts = {};
8+
9+
for (const item of items) {
10+
counts[item] = (counts[item] || 0) + 1;
11+
}
12+
13+
return counts;
14+
15+
}
216

317
module.exports = tally;

0 commit comments

Comments
 (0)