Skip to content

Commit e4288da

Browse files
complete feature/sprint-2
1 parent cfd674c commit e4288da

File tree

16 files changed

+205
-25
lines changed

16 files changed

+205
-25
lines changed

Sprint-2/debug/address.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,8 @@ 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"]}`);
16+
17+
// Code returns undefined
18+
// because address is an object but it is accessed like an array
19+
// in an object values can be accessed by using their proper keys

Sprint-2/debug/author.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ const author = {
1111
alive: true,
1212
};
1313

14-
for (const value of author) {
15-
console.log(value);
14+
for (const key in author) {
15+
console.log(author[key]);
1616
}
17+
18+
// for iterating in an object for...in can be used
19+
// and each iteration it returns the key
20+
// then each value can be accessed

Sprint-2/debug/recipe.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ const recipe = {
1010
ingredients: ["olive oil", "tomatoes", "salt", "pepper"],
1111
};
1212

13-
console.log(`${recipe.title} serves ${recipe.serves}
14-
ingredients:
15-
${recipe}`);
13+
console.log(`${recipe.title} serves ${recipe.serves}\ningredients:`);
14+
recipe.ingredients.forEach((ingredient) => console.log(ingredient));
15+
16+
// everything is logged on one line
17+
// console.log is logging the recipe object array as a whole
18+
// instead of each value of ingredients array
19+
// the ingredients array must be logged using a loop

Sprint-2/implement/contains.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1-
function contains() {}
1+
function contains(obj, property) {
2+
try {
3+
const keys = Object.keys(obj);
4+
return keys.includes(property);
5+
} catch (error) {
6+
throw new Error("The parameter given is not a plain JS object.");
7+
}
8+
}
29

310
module.exports = contains;

Sprint-2/implement/contains.test.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,32 @@ 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 empty object returns false", () =>
24+
expect(contains({}, "key1")).toEqual(false));
2425

2526
// Given an object with properties
2627
// When passed to contains with an existing property name
2728
// Then it should return true
29+
test("contains returns true when object contains the given property", () =>
30+
expect(contains({ key1: "value1", key2: "value2" }, "key1")).toEqual(true));
2831

2932
// Given an object with properties
3033
// When passed to contains with a non-existent property name
3134
// Then it should return false
35+
test("contains returns false when object does not contain the given property", () =>
36+
expect(contains({ key1: "value1", key2: "value2" }, "key4")).toEqual(false));
3237

3338
// Given invalid parameters like an array
3439
// When passed to contains
3540
// Then it should return false or throw an error
41+
it("contains returns false or throws an error if given parameter is not a valid object", () => {
42+
expect(contains([], "key1")).toEqual(false);
43+
expect(contains("key1:value1", "key1")).toEqual(false);
44+
expect(contains(5235, "key1")).toEqual(false);
45+
expect(() => contains(undefined, "key1")).toThrow(
46+
"The parameter given is not a plain JS object."
47+
);
48+
expect(() => contains(null, "key1")).toThrow(
49+
"The parameter given is not a plain JS object."
50+
);
51+
});

Sprint-2/implement/lookup.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
function createLookup() {
2-
// implementation here
1+
function createLookup(countryCurrencyPairs) {
2+
if (!Array.isArray(countryCurrencyPairs)) return {};
3+
return countryCurrencyPairs.reduce((acc, curr) => {
4+
acc[curr[0]] = curr[1];
5+
return acc;
6+
}, {});
37
}
48

59
module.exports = createLookup;

Sprint-2/implement/lookup.test.js

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

3-
test.todo("creates a country currency code lookup for multiple codes");
4-
53
/*
64
75
Create a lookup object of key value pairs from an array of code pairs
@@ -33,3 +31,24 @@ It should return:
3331
'CA': 'CAD'
3432
}
3533
*/
34+
35+
describe("createLookup", () => {
36+
it("returns empty object if the parameter passed is not an array", () => {
37+
expect(createLookup("US:USD")).toEqual({});
38+
expect(createLookup(undefined)).toEqual({});
39+
});
40+
41+
it("returns an object of country initials and currency code", () =>
42+
expect(
43+
createLookup([
44+
["US", "USD"],
45+
["CA", "CAD"],
46+
])
47+
).toEqual({
48+
US: "USD",
49+
CA: "CAD",
50+
}));
51+
52+
it("returns an empty object if passed an empty array", () =>
53+
expect(createLookup([])).toEqual({}));
54+
});

Sprint-2/implement/querystring.js

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,29 @@ function parseQueryString(queryString) {
66
const keyValuePairs = queryString.split("&");
77

88
for (const pair of keyValuePairs) {
9-
const [key, value] = pair.split("=");
10-
queryParams[key] = value;
9+
let key = "",
10+
value = "";
11+
let equalSignIndex = pair.indexOf("=");
12+
13+
if (equalSignIndex === -1) {
14+
key = pair;
15+
value = "";
16+
} else {
17+
key = pair.slice(0, equalSignIndex);
18+
value = pair.slice(equalSignIndex + 1);
19+
}
20+
21+
const existingKeys = Object.keys(queryParams);
22+
if (key === "" && value === "") continue;
23+
if (existingKeys.includes(key)) {
24+
if (queryParams[key] === value) continue;
25+
if (Array.isArray(queryParams[key])) {
26+
queryParams[key].push(value);
27+
} else {
28+
const temp = queryParams[key];
29+
queryParams[key] = [temp, value];
30+
}
31+
} else queryParams[key] = value;
1132
}
1233

1334
return queryParams;

Sprint-2/implement/querystring.test.js

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,39 @@
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 querystring values containing repetitive keys", () => {
15+
expect(parseQueryString("a=1&b=6&a=2&a=3&b=7")).toEqual({
16+
a: ["1", "2", "3"],
17+
b: ["6", "7"],
18+
});
19+
});
20+
21+
test("parses querystring values containing repetitive keys with same values", () => {
22+
expect(parseQueryString("a=1&b=6&a=2&a=3&b=6")).toEqual({
23+
a: ["1", "2", "3"],
24+
b: "6",
25+
});
26+
});
27+
28+
test("parses querystring values containing no key and value but only =", () => {
29+
expect(parseQueryString("=&b=6&a=2&=&=")).toEqual({
30+
a: "2",
31+
b: "6",
32+
});
33+
});
34+
35+
test("parses querystring values missing = sign", () => {
36+
expect(parseQueryString("id=5&name=mohsen&age")).toEqual({
37+
id: "5",
38+
name: "mohsen",
39+
age: "",
1140
});
1241
});

Sprint-2/implement/tally.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1-
function tally() {}
1+
function tally(list) {
2+
if (!Array.isArray(list)) throw new Error("Not an array.");
3+
return list.reduce((acc, curr) => {
4+
acc[curr] = (acc[curr] || 0) + 1;
5+
return acc;
6+
}, {});
7+
}
28

39
module.exports = tally;

0 commit comments

Comments
 (0)