Skip to content

Commit 917e563

Browse files
committed
Done the coursework exercises
1 parent 96d077b commit 917e563

12 files changed

Lines changed: 148 additions & 15 deletions

File tree

Sprint-2/debug/address.js

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

3+
// This code will fail since we have put Index 0 instead of the key houseNumber
4+
35
// This code should log out the houseNumber from the address object
46
// but it isn't working...
57
// Fix anything that isn't working
@@ -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
@@ -1,5 +1,7 @@
11
// Predict and explain first...
22

3+
//for...of cannot iterate plain objects directly, so it will display an error
4+
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
57

@@ -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: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// Predict and explain first...
22

3+
// It will print the whole object instead of the ingredients list. To get it one below the other, we need to join the array and then \n to get it on separate lines
4+
35
// This program should log out the title, how many it serves and the ingredients.
46
// Each ingredient should be logged on a new line
57
// How can you fix it?
@@ -11,5 +13,5 @@ const recipe = {
1113
};
1214

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

Sprint-2/implement/contains.js

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

38
module.exports = contains;

Sprint-2/implement/contains.test.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,41 @@ 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("returns false when object is empty", () => {
24+
expect(contains({}, "anyProp")).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("returns true for object with existing property name", () => {
31+
expect(contains({ name: "Brad" }, "name")).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("returns false for object with non-existent property name", () => {
38+
expect(contains({ name: "Brad" }, "age")).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("returns false when input is an array", () => {
45+
expect(contains(["Brad"], "0")).toBe(false);
46+
});
47+
48+
// Given null inputs
49+
// When passed to contains
50+
// Then it should return false
51+
test("returns false when input is null", () => {
52+
expect(contains(null, "name")).toBe(false);
53+
});
54+
55+
// Given non object inputs like string or number
56+
// When passed to contains
57+
// Then it should return false
58+
test("returns false when input is a string", () => {
59+
expect(contains("Brad", "name")).toBe(false);
60+
});

Sprint-2/implement/lookup.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
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+
lookup[countryCode] = currencyCode;
8+
}
9+
10+
return lookup;
311
}
412

513
module.exports = createLookup;

Sprint-2/implement/lookup.test.js

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

15+
test("creates a lookup for 1 pair", () => {
16+
const input = [["IN", "INR"]];
17+
const result = createLookup(input);
18+
expect(result).toEqual({
19+
IN: "INR",
20+
});
21+
});
22+
23+
test("returns an empty object when input array is empty", () => {
24+
const input = [];
25+
const result = createLookup(input);
26+
expect(result).toEqual({});
27+
});
28+
29+
test("ignores cases where it is invalid within other pairs", () => {
30+
const input = [["US", "USD"], ["Invalid"], ["CA", "CAD"]];
31+
const result = createLookup(input);
32+
expect(result).toEqual({
33+
US: "USD",
34+
CA: "CAD",
35+
});
36+
});
537
/*
638
739
Create a lookup object of key value pairs from an array of code pairs

Sprint-2/implement/querystring.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ function parseQueryString(queryString) {
66
const keyValuePairs = queryString.split("&");
77

88
for (const pair of keyValuePairs) {
9-
const [key, value] = pair.split("=");
9+
const index = pair.indexOf("=");
10+
const key = pair.slice(0, index);
11+
const value = pair.slice(index + 1);
1012
queryParams[key] = value;
1113
}
1214

Sprint-2/implement/querystring.test.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,22 @@
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-
const parseQueryString = require("./querystring.js")
6+
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",
11+
});
12+
});
13+
14+
test("parses with missing value", () => {
15+
expect(parseQueryString("a=")).toEqual({
16+
a: "",
17+
});
18+
});
19+
20+
test("parses with missing key", () => {
21+
expect(parseQueryString("=value")).toEqual({
22+
"": "value",
1123
});
1224
});

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 result = {};
7+
8+
for (const item of items) {
9+
if (result[item]) {
10+
result[item]++;
11+
} else {
12+
result[item] = 1;
13+
}
14+
}
15+
16+
return result;
17+
}
218

319
module.exports = tally;

0 commit comments

Comments
 (0)