Skip to content

Commit fd0a7da

Browse files
Complete Sprint 2 debug and implement exercises
1 parent 4222cda commit fd0a7da

10 files changed

Lines changed: 107 additions & 16 deletions

File tree

‎Sprint-2/debug/address.js‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
// Predict and explain first...
22

33
// This code should log out the houseNumber from the address object
4+
45
// but it isn't working...
6+
57
// Fix anything that isn't working
68

79
const address = {
@@ -12,4 +14,4 @@ const address = {
1214
postcode: "XYZ 123",
1315
};
1416

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

‎Sprint-2/debug/author.js‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

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
5+
// The problem is that the for...of loop is being used on an object,
6+
// which is not iterable. Instead, we should use Object.values() to get an array of the object's values and then iterate over that array.
57

68
const author = {
79
firstName: "Zadie",
@@ -11,6 +13,6 @@ const author = {
1113
alive: true,
1214
};
1315

14-
for (const value of author) {
16+
for (const value of Object.values(author)) {
1517
console.log(value);
1618
}

‎Sprint-2/debug/recipe.js‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
// Predict and explain first...
2+
//
23

34
// This program should log out the title, how many it serves and the ingredients.
45
// Each ingredient should be logged on a new line
56
// How can you fix it?
7+
// The problem is that the recipe object is being logged directly, which will not display the ingredients in the desired format.
8+
// Instead, we should iterate over the ingredients array and log each ingredient on a new line.
69

710
const recipe = {
811
title: "bruschetta",
@@ -11,5 +14,7 @@ const recipe = {
1114
};
1215

1316
console.log(`${recipe.title} serves ${recipe.serves}
14-
ingredients:
15-
${recipe}`);
17+
ingredients:`);
18+
recipe.ingredients.forEach((ingredient) => {
19+
console.log(`- ${ingredient}`);
20+
});

‎Sprint-2/implement/contains.js‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1-
function contains() {}
1+
function contains(object, propertyName) {
2+
if (Array.isArray(object)) {
3+
return false;
4+
}
25

3-
module.exports = contains;
6+
return Object.hasOwn(object, propertyName);
7+
}
8+
9+
module.exports = contains;

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,28 @@ 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 an empty object returns false", () => {
24+
expect(contains({}, "propertyName")).toBe(false);
25+
});
26+
2427

2528
// Given an object with properties
2629
// When passed to contains with an existing property name
2730
// Then it should return true
31+
test("returns true when the property exists", () => {
32+
expect(contains({ a: 1, b: 2 }, "a")).toBe(true);
33+
});
2834

2935
// Given an object with properties
3036
// When passed to contains with a non-existent property name
3137
// Then it should return false
38+
test("returns false when the property doesn't exist", () => {
39+
expect(contains({ a: 1, b: 2 }, "c")).toBe(false);
40+
});
3241

3342
// Given invalid parameters like an array
3443
// When passed to contains
3544
// Then it should return false or throw an error
45+
test("returns false for invalid input types", () => {
46+
expect(contains([1, 2, 3], "propertyName")).toBe(false);
47+
});

‎Sprint-2/implement/lookup.js‎

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

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

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
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+
const countryCurrencyPairs = [
5+
["US", "USD"],
6+
["CA", "CAD"],
7+
];
8+
9+
expect(createLookup(countryCurrencyPairs)).toEqual({
10+
US: "USD",
11+
CA: "CAD",
12+
});
13+
});
414

515
/*
616

‎Sprint-2/implement/querystring.js‎

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,35 @@
11
function parseQueryString(queryString) {
22
const queryParams = {};
3+
34
if (queryString.length === 0) {
45
return queryParams;
56
}
7+
68
const keyValuePairs = queryString.split("&");
79

810
for (const pair of keyValuePairs) {
9-
const [key, value] = pair.split("=");
10-
queryParams[key] = value;
11+
if (pair === "") {
12+
continue;
13+
}
14+
15+
const separatorIndex = pair.indexOf("=");
16+
17+
const encodedKey =
18+
separatorIndex === -1 ? pair : pair.slice(0, separatorIndex);
19+
20+
const encodedValue =
21+
separatorIndex === -1 ? "" : pair.slice(separatorIndex + 1);
22+
23+
const key = decodeURIComponent(encodedKey.replaceAll("+", " "));
24+
const value = decodeURIComponent(encodedValue.replaceAll("+", " "));
25+
26+
if (!Object.hasOwn(queryParams, key)) {
27+
queryParams[key] = value;
28+
} else if (Array.isArray(queryParams[key])) {
29+
queryParams[key].push(value);
30+
} else {
31+
queryParams[key] = [queryParams[key], value];
32+
}
1133
}
1234

1335
return queryParams;

‎Sprint-2/implement/tally.js‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1-
function tally() {}
1+
function tally(items) {
2+
if (!Array.isArray(items)) {
3+
throw new TypeError("Expected an array");
4+
}
25

3-
module.exports = tally;
6+
const counts = {};
7+
8+
for (const item of items) {
9+
counts[item] = (counts[item] || 0) + 1;
10+
}
11+
12+
return counts;
13+
}
14+
15+
module.exports = tally;

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,21 @@ const tally = require("./tally.js");
2323
// Given an empty array
2424
// When passed to tally
2525
// Then it should return an empty object
26-
test.todo("tally on an empty array returns an empty object");
26+
test("tally on an empty array returns an empty object", () => {
27+
expect(tally([])).toEqual({});
28+
});
29+
30+
test("returns the count for each unique item", () => {
31+
expect(tally(["a", "a", "b", "c"])).toEqual({
32+
a: 2,
33+
b: 1,
34+
c: 1,
35+
});
36+
});
37+
38+
test("throws an error when passed a string", () => {
39+
expect(() => tally("a, a, b")).toThrow(TypeError);
40+
});
2741

2842
// Given an array with duplicate items
2943
// When passed to tally

0 commit comments

Comments
 (0)