Skip to content

Commit ddab5f8

Browse files
committed
Add Sprint-2 work only
1 parent 4222cda commit ddab5f8

13 files changed

Lines changed: 225 additions & 33 deletions

File tree

‎Sprint-2/debug/address.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Predict and explain first...
2-
2+
//code won't work because it's looking for the house number in the wrong place
33
// This code should log out the houseNumber from the address object
44
// but it isn't working...
55
// Fix anything that isn't working
@@ -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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Predict and explain first...
2-
2+
//will give an error because author is an object, and you can't use a for...of loop directly on an object
33
// This program attempts to log out all the property values in the object.
44
// But it isn't working. Explain why first and then fix the problem
55

@@ -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: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Predict and explain first...
2-
2+
//it won't print the ingredients properly because recipe is an object
33
// This program should log out the title, how many it serves and the ingredients.
44
// Each ingredient should be logged on a new line
55
// How can you fix it?
@@ -11,5 +11,4 @@ const recipe = {
1111
};
1212

1313
console.log(`${recipe.title} serves ${recipe.serves}
14-
ingredients:
15-
${recipe}`);
14+
Ingredients:${recipe.ingredients.join("\n")}`);

‎Sprint-2/implement/contains.js‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1-
function contains() {}
1+
function contains(object, property) {
2+
if (typeof object !== "object" || object === null || Array.isArray(object)) {
3+
return false;
4+
}
5+
6+
return object.hasOwnProperty(property);
7+
}
28

39
module.exports = contains;

‎Sprint-2/implement/contains.test.js‎

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,35 @@ E.g. contains({a: 1, b: 2}, 'a') // returns true
88
as the object contains a key of 'a'
99
1010
E.g. contains({a: 1, b: 2}, 'c') // returns false
11-
as the object doesn't contains a key of 'c'
11+
as the object doesn't contain a key of 'c'
1212
*/
1313

1414
// Acceptance criteria:
1515

16-
// Given a contains function
17-
// When passed an object and a property name
18-
// Then it should return true if the object contains the property, false otherwise
19-
2016
// Given an empty object
2117
// When passed to contains
2218
// Then it should return false
23-
test.todo("contains on empty object returns false");
19+
test("contains on empty object returns false", () => {
20+
expect(contains({}, "a")).toBe(false);
21+
});
2422

2523
// Given an object with properties
2624
// When passed to contains with an existing property name
2725
// Then it should return true
26+
test("contains returns true when property exists", () => {
27+
expect(contains({ a: 1, b: 2 }, "a")).toBe(true);
28+
});
2829

2930
// Given an object with properties
3031
// When passed to contains with a non-existent property name
3132
// Then it should return false
33+
test("contains returns false when property does not exist", () => {
34+
expect(contains({ a: 1, b: 2 }, "c")).toBe(false);
35+
});
3236

3337
// Given invalid parameters like an array
3438
// When passed to contains
3539
// Then it should return false or throw an error
40+
test("contains returns false when passed an array", () => {
41+
expect(contains([], "length")).toBe(false);
42+
});

‎Sprint-2/implement/lookup.js‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
1-
function createLookup() {
2-
// implementation here
1+
function createLookup(countryCurrencyPairs) {
2+
const lookup = {};
3+
4+
for (const pair of countryCurrencyPairs) {
5+
const countryCode = pair[0];
6+
const currencyCode = pair[1];
7+
8+
lookup[countryCode] = currencyCode;
9+
}
10+
11+
return lookup;
312
}
413

514
module.exports = createLookup;

‎Sprint-2/implement/lookup.test.js‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,14 @@ It should return:
3333
'CA': 'CAD'
3434
}
3535
*/
36+
test("creates a country currency code lookup for multiple codes", () => {
37+
const countryCurrencyPairs = [
38+
["US", "USD"],
39+
["CA", "CAD"],
40+
];
41+
42+
expect(createLookup(countryCurrencyPairs)).toEqual({
43+
US: "USD",
44+
CA: "CAD",
45+
});
46+
});

‎Sprint-2/implement/querystring.js‎

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,47 @@
11
function parseQueryString(queryString) {
22
const queryParams = {};
3+
4+
if (queryString === null || queryString === undefined) {
5+
return queryParams;
6+
}
7+
38
if (queryString.length === 0) {
49
return queryParams;
510
}
11+
612
const keyValuePairs = queryString.split("&");
713

814
for (const pair of keyValuePairs) {
9-
const [key, value] = pair.split("=");
10-
queryParams[key] = value;
15+
// Ignore empty pairs
16+
if (pair === "") {
17+
continue;
18+
}
19+
20+
// Split only on the first "="
21+
const [keyPart, ...valueParts] = pair.split("=");
22+
23+
let key = keyPart;
24+
let value = valueParts.join("=");
25+
26+
// If there is no "=" the value should be empty
27+
if (valueParts.length === 0) {
28+
value = "";
29+
}
30+
31+
// Replace + with spaces and decode URL encoding
32+
key = decodeURIComponent(key.replace(/\+/g, " "));
33+
value = decodeURIComponent(value.replace(/\+/g, " "));
34+
35+
// Handle duplicate keys
36+
if (Object.prototype.hasOwnProperty.call(queryParams, key)) {
37+
if (Array.isArray(queryParams[key])) {
38+
queryParams[key].push(value);
39+
} else {
40+
queryParams[key] = [queryParams[key], value];
41+
}
42+
} else {
43+
queryParams[key] = value;
44+
}
1145
}
1246

1347
return queryParams;

‎Sprint-2/implement/querystring.test.js‎

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// Below are some test cases the implementation doesn't handle well.
44
// Fix the implementation for these tests, and try to think of as many other edge cases as possible - write tests and fix those too.
55

6-
const parseQueryString = require("./querystring.js")
6+
const parseQueryString = require("./querystring.js");
77

88
test("should parse values containing '='", () => {
99
expect(parseQueryString("equation=a=b-2")).toEqual({
@@ -46,3 +46,37 @@ test("should store values of a key in an array when the key has 2 or more values
4646
foo: "bar",
4747
});
4848
});
49+
50+
test("should return an empty object for an empty query string", () => {
51+
expect(parseQueryString("")).toEqual({});
52+
});
53+
54+
test("should handle values containing multiple equals signs", () => {
55+
expect(parseQueryString("url=a=b=c")).toEqual({
56+
url: "a=b=c",
57+
});
58+
});
59+
60+
test("should ignore multiple empty pairs", () => {
61+
expect(parseQueryString("&&foo=bar&&")).toEqual({
62+
foo: "bar",
63+
});
64+
});
65+
66+
test("should decode spaces using percent encoding", () => {
67+
expect(parseQueryString("name=John%20Doe")).toEqual({
68+
name: "John Doe",
69+
});
70+
});
71+
72+
test("should handle empty values with duplicate keys", () => {
73+
expect(parseQueryString("key=&key=")).toEqual({
74+
key: ["", ""],
75+
});
76+
});
77+
78+
test("should handle duplicate empty keys", () => {
79+
expect(parseQueryString("=one&=two")).toEqual({
80+
"": ["one", "two"],
81+
});
82+
});

‎Sprint-2/implement/tally.js‎

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

319
module.exports = tally;

0 commit comments

Comments
 (0)