Skip to content

Commit 4b1481f

Browse files
committed
Complete Sprint 2 exercises
1 parent 96d077b commit 4b1481f

13 files changed

Lines changed: 160 additions & 42 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+
// The address variable is an object literal containing key–value pairs separated by commas. The code originally tries to access address[0], which would only work if the data were in an array. Since address is an object and not an array, there is no index 0, so it returns undefined. To fix the problem we must access the property using its key, for example address.houseNumber.
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+
// The program does not work because for...of can only be used on iterable objects such as arrays or strings. The author variable is an object, which is not iterable by default. To fix the problem we can use Object.values(author) to convert the object values into an iterable array and then loop through them.
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: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Predict and explain first...
2-
2+
// The code does not work because ${recipe} prints the entire object rather than the ingredients array. To display each ingredient on a new line, we need to loop through recipe.ingredients, which is an array. Using a for...of loop allows us to print each ingredient individually.
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,9 @@ const recipe = {
1111
};
1212

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

Sprint-2/implement/contains.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1-
function contains() {}
1+
function contains(object, property) {
22

3-
module.exports = contains;
3+
// check if parameter is a valid object and not an array
4+
if (typeof object !== "object" || Array.isArray(object) || object === null) {
5+
return false;
6+
}
7+
8+
return Object.prototype.hasOwnProperty.call(object, property);
9+
}
10+
11+
module.exports = contains;
Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,31 @@
11
const contains = require("./contains.js");
22

3-
/*
4-
Implement a function called contains that checks an object contains a
5-
particular property
6-
7-
E.g. contains({a: 1, b: 2}, 'a') // returns true
8-
as the object contains a key of 'a'
9-
10-
E.g. contains({a: 1, b: 2}, 'c') // returns false
11-
as the object doesn't contains a key of 'c'
12-
*/
13-
14-
// Acceptance criteria:
15-
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-
203
// Given an empty object
214
// When passed to contains
225
// Then it should return false
23-
test.todo("contains on empty object returns false");
6+
test("contains on empty object returns false", () => {
7+
expect(contains({}, "a")).toBe(false);
8+
});
249

2510
// Given an object with properties
2611
// When passed to contains with an existing property name
2712
// Then it should return true
13+
test("returns true when object contains the property", () => {
14+
const obj = { a: 1, b: 2 };
15+
expect(contains(obj, "a")).toBe(true);
16+
});
2817

2918
// Given an object with properties
3019
// When passed to contains with a non-existent property name
3120
// Then it should return false
21+
test("returns false when object does not contain the property", () => {
22+
const obj = { a: 1, b: 2 };
23+
expect(contains(obj, "c")).toBe(false);
24+
});
3225

3326
// Given invalid parameters like an array
3427
// When passed to contains
35-
// Then it should return false or throw an error
28+
// Then it should return false
29+
test("returns false when passed invalid parameters like an array", () => {
30+
expect(contains([1, 2, 3], "a")).toBe(false);
31+
});

Sprint-2/implement/lookup.js

Lines changed: 12 additions & 3 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

5-
module.exports = createLookup;
14+
module.exports = createLookup;

Sprint-2/implement/lookup.test.js

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

3-
test.todo("creates a country currency code lookup for multiple codes");
3+
test("creates a country currency code lookup for multiple codes", () => {
4+
5+
const countryCurrencyPairs = [
6+
["US", "USD"],
7+
["CA", "CAD"]
8+
];
9+
10+
const result = createLookup(countryCurrencyPairs);
11+
12+
expect(result).toEqual({
13+
US: "USD",
14+
CA: "CAD"
15+
});
16+
17+
});
418

519
/*
620
@@ -32,4 +46,4 @@ It should return:
3246
'US': 'USD',
3347
'CA': 'CAD'
3448
}
35-
*/
49+
*/

Sprint-2/implement/querystring.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,30 @@
11
function parseQueryString(queryString) {
22
const queryParams = {};
3+
4+
// If the query string is empty return empty object
35
if (queryString.length === 0) {
46
return queryParams;
57
}
8+
69
const keyValuePairs = queryString.split("&");
710

811
for (const pair of keyValuePairs) {
9-
const [key, value] = pair.split("=");
12+
13+
const index = pair.indexOf("=");
14+
15+
// If no "=" exists treat the value as empty
16+
if (index === -1) {
17+
queryParams[pair] = "";
18+
continue;
19+
}
20+
21+
const key = pair.slice(0, index);
22+
const value = pair.slice(index + 1);
23+
1024
queryParams[key] = value;
1125
}
1226

1327
return queryParams;
1428
}
1529

16-
module.exports = parseQueryString;
30+
module.exports = parseQueryString;

Sprint-2/implement/querystring.test.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,29 @@ const parseQueryString = require("./querystring.js")
77

88
test("parses querystring values containing =", () => {
99
expect(parseQueryString("equation=x=y+1")).toEqual({
10-
"equation": "x=y+1",
10+
equation: "x=y+1",
1111
});
1212
});
13+
14+
test("returns empty object for empty query string", () => {
15+
expect(parseQueryString("")).toEqual({});
16+
});
17+
18+
test("parses multiple parameters", () => {
19+
expect(parseQueryString("name=John&age=30")).toEqual({
20+
name: "John",
21+
age: "30",
22+
});
23+
});
24+
25+
test("handles parameter with empty value", () => {
26+
expect(parseQueryString("name=")).toEqual({
27+
name: "",
28+
});
29+
});
30+
31+
test("handles parameter without equals sign", () => {
32+
expect(parseQueryString("name")).toEqual({
33+
name: "",
34+
});
35+
});

Sprint-2/implement/tally.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,23 @@
1-
function tally() {}
1+
function tally(items) {
22

3-
module.exports = tally;
3+
if (!Array.isArray(items)) {
4+
throw new Error("Input must be an array");
5+
}
6+
7+
const result = {};
8+
9+
for (const item of items) {
10+
11+
if (result[item]) {
12+
result[item] += 1;
13+
} else {
14+
result[item] = 1;
15+
}
16+
17+
}
18+
19+
return result;
20+
21+
}
22+
23+
module.exports = tally;

0 commit comments

Comments
 (0)