Skip to content

Commit 66308a3

Browse files
committed
implement
1 parent 96d077b commit 66308a3

11 files changed

Lines changed: 159 additions & 12 deletions

File tree

Sprint-2/debug/address.js

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

3+
// This will log "My house number is undefined" because
4+
// address[0] is looking for index 0 like it's an array, but address
5+
// is an object. To get a value from an object you need to use the
6+
// key name, not a number. Changing address[0] to address.houseNumber
7+
// will fix it and log "My house number is 42".
8+
39
// This code should log out the houseNumber from the address object
410
// but it isn't working...
511
// Fix anything that isn't working
@@ -12,4 +18,4 @@ const address = {
1218
postcode: "XYZ 123",
1319
};
1420

15-
console.log(`My house number is ${address[0]}`);
21+
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
@@ -1,4 +1,6 @@
11
// Predict and explain first...
2+
// This code will throw a TypeError: "author is not iterable".
3+
//
24

35
// This program attempts to log out all the property values in the object.
46
// But it isn't working. Explain why first and then fix the problem
@@ -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: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
11
// Predict and explain first...
2+
// This will not log the ingredients correctly. It will print the whole
3+
// recipe object as [object Object] instead of each ingredient on its own line,
4+
// because you're interpolating the entire object (`${recipe}`) instead of
5+
// its properties and the ingredients array items.
26

37
// This program should log out the title, how many it serves and the ingredients.
48
// Each ingredient should be logged on a new line
59
// How can you fix it?
610

11+
712
const recipe = {
813
title: "bruschetta",
914
serves: 2,
1015
ingredients: ["olive oil", "tomatoes", "salt", "pepper"],
1116
};
1217

1318
console.log(`${recipe.title} serves ${recipe.serves}
14-
ingredients:
15-
${recipe}`);
19+
ingredients:`);
20+
21+
for (const ingredient of recipe.ingredients) {
22+
console.log(ingredient);
23+
}

Sprint-2/implement/contains.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1-
function contains() {}
1+
function contains(obj, propName) {
2+
// Return false for non-objects or null
3+
if (obj === null || typeof obj !== "object" || Array.isArray(obj)) {
4+
return false;
5+
}
6+
7+
// Check if the object has the property as its own key
8+
return Object.hasOwn(obj, propName);
9+
}
210

311
module.exports = contains;
Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,55 @@
11
const contains = require("./contains.js");
22

3+
34
/*
45
Implement a function called contains that checks an object contains a
56
particular property
67
8+
79
E.g. contains({a: 1, b: 2}, 'a') // returns true
810
as the object contains a key of 'a'
911
12+
1013
E.g. contains({a: 1, b: 2}, 'c') // returns false
1114
as the object doesn't contains a key of 'c'
1215
*/
1316

17+
1418
// Acceptance criteria:
1519

20+
1621
// Given a contains function
1722
// When passed an object and a property name
1823
// Then it should return true if the object contains the property, false otherwise
1924

25+
2026
// Given an empty object
2127
// When passed to contains
2228
// Then it should return false
23-
test.todo("contains on empty object returns false");
29+
test("contains on empty object returns false", () => {
30+
expect(contains({}, "a")).toBe(false);
31+
});
32+
2433

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

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

3350
// Given invalid parameters like an array
3451
// When passed to contains
3552
// Then it should return false or throw an error
53+
test("contains with invalid parameters returns false", () => {
54+
expect(contains([], "a")).toBe(false);
55+
});

Sprint-2/implement/lookup.js

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

512
module.exports = createLookup;

Sprint-2/implement/lookup.test.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,54 @@
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+
["GB", "GBP"],
8+
];
9+
10+
const result = createLookup(countryCurrencyPairs);
11+
12+
expect(result).toEqual({
13+
US: "USD",
14+
CA: "CAD",
15+
GB: "GBP",
16+
});
17+
});
18+
419

520
/*
621
22+
723
Create a lookup object of key value pairs from an array of code pairs
824
25+
926
Acceptance Criteria:
1027
28+
1129
Given
1230
- An array of arrays representing country code and currency code pairs
1331
e.g. [['US', 'USD'], ['CA', 'CAD']]
1432
33+
1534
When
1635
- createLookup function is called with the country-currency array as an argument
1736
37+
1838
Then
1939
- It should return an object where:
2040
- The keys are the country codes
2141
- The values are the corresponding currency codes
2242
43+
2344
Example
2445
Given: [['US', 'USD'], ['CA', 'CAD']]
2546
47+
2648
When
2749
createLookup(countryCurrencyPairs) is called
2850
51+
2952
Then
3053
It should return:
3154
{

Sprint-2/implement/querystring.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,32 @@ function parseQueryString(queryString) {
33
if (queryString.length === 0) {
44
return queryParams;
55
}
6+
67
const keyValuePairs = queryString.split("&");
78

89
for (const pair of keyValuePairs) {
9-
const [key, value] = pair.split("=");
10+
if (pair === "") {
11+
continue;
12+
}
13+
14+
const indexOfEquals = pair.indexOf("=");
15+
let key;
16+
let value;
17+
18+
if (indexOfEquals === -1) {
19+
// no "=", treat whole pair as key with empty value
20+
key = pair;
21+
value = "";
22+
} else {
23+
key = pair.slice(0, indexOfEquals);
24+
value = pair.slice(indexOfEquals + 1);
25+
}
26+
1027
queryParams[key] = value;
1128
}
1229

1330
return queryParams;
1431
}
1532

33+
1634
module.exports = parseQueryString;

Sprint-2/implement/querystring.test.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,34 @@
33
// Below is one test case for an edge case the implementation doesn't handle well.
44
// Fix the implementation for this test, and try to think of as many other edge cases as possible - write tests and fix those too.
55

6+
67
const parseQueryString = require("./querystring.js")
78

9+
810
test("parses querystring values containing =", () => {
911
expect(parseQueryString("equation=x=y+1")).toEqual({
1012
"equation": "x=y+1",
1113
});
1214
});
15+
16+
test("parses empty querystring as empty object", () => {
17+
expect(parseQueryString("")).toEqual({});
18+
});
19+
20+
test("parses single key with empty value", () => {
21+
expect(parseQueryString("flag=")).toEqual({ flag: "" });
22+
});
23+
24+
test("parses key with no =", () => {
25+
expect(parseQueryString("flag")).toEqual({ flag: "" });
26+
});
27+
28+
test("parses multiple key value pairs", () => {
29+
expect(parseQueryString("a=1&b=2")).toEqual({ a: "1", b: "2" });
30+
});
31+
32+
test("later duplicate keys overwrite earlier ones", () => {
33+
expect(parseQueryString("a=1&a=2")).toEqual({ a: "2" });
34+
});
35+
36+

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 Error("tally expects 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;

0 commit comments

Comments
 (0)