Skip to content

Commit 3d3aabb

Browse files
committed
Completed all exercises in implement directory
1 parent 254f0b4 commit 3d3aabb

File tree

7 files changed

+89
-10
lines changed

7 files changed

+89
-10
lines changed

Sprint-2/implement/contains.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1-
function contains() {}
1+
function contains(object, name) {
2+
if (object == null || Object.keys(object).length === 0) {
3+
return false;
4+
}
5+
6+
for (let key in object) {
7+
if (key === name) {
8+
return true;
9+
}
10+
}
11+
12+
return false;
13+
}
214

315
module.exports = contains;

Sprint-2/implement/contains.test.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ 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");
2423

2524
// Given an object with properties
2625
// When passed to contains with an existing property name
@@ -33,3 +32,15 @@ test.todo("contains on empty object returns false");
3332
// Given invalid parameters like an array
3433
// When passed to contains
3534
// Then it should return false or throw an error
35+
36+
describe("contains", () => {
37+
[
38+
{ input: [{}, "a"], expected: false },
39+
{ input: [{ a: 1, b: 2 }, "a"], expected: true },
40+
{ input: [{ a: 1, b: 2 }, "c"], expected: false },
41+
{ input: [null, "a"], expected: false },
42+
].forEach(({ input, expected }) =>
43+
it(`return if object contains the key for ${input[1]}`, () =>
44+
expect(contains(...input)).toEqual(expected))
45+
);
46+
});

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 array = {};
3+
4+
for (const pair of countryCurrencyPairs) {
5+
const country = pair[0];
6+
const currency = pair[1];
7+
array[country] = currency;
8+
}
9+
10+
return array;
311
}
412

513
module.exports = createLookup;

Sprint-2/implement/querystring.js

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

88
for (const pair of keyValuePairs) {
9-
const [key, value] = pair.split("=");
9+
const [key, ...rest] = pair.split("=");
10+
const value = rest.join("=");
1011
queryParams[key] = value;
1112
}
1213

Sprint-2/implement/querystring.test.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,23 @@
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

8-
test("parses querystring values containing =", () => {
9-
expect(parseQueryString("equation=x=y+1")).toEqual({
10-
"equation": "x=y+1",
8+
describe("parseQueryString", () => {
9+
test("returns an empty object when query is empty", () => {
10+
expect(parseQueryString("")).toEqual({});
11+
});
12+
13+
test("parses query string values containing =", () => {
14+
expect(parseQueryString("equation=x=y+1")).toEqual({
15+
equation: "x=y+1",
16+
});
17+
});
18+
19+
test("checks for keys without values", () => {
20+
expect(parseQueryString("foo&bar=2")).toEqual({
21+
foo: "",
22+
bar: "2",
23+
});
1124
});
1225
});

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(itemsList) {
2+
const counts = {};
3+
4+
if (itemsList.length === 0) {
5+
return {};
6+
}
7+
8+
if (!Array.isArray(itemsList)) {
9+
throw new Error("Input must be an array");
10+
}
11+
12+
for (const item of itemsList) {
13+
counts[item] = (counts[item] || 0) + 1;
14+
}
15+
16+
return counts;
17+
}
218

319
module.exports = tally;

Sprint-2/implement/tally.test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,21 @@ test.todo("tally on an empty array returns an empty object");
3232
// Given an invalid input like a string
3333
// When passed to tally
3434
// Then it should throw an error
35+
36+
describe("tally", () => {
37+
test("returns an empty object when given an empty array", () => {
38+
expect(tally([])).toEqual({});
39+
});
40+
41+
test("returns counts for each unique item", () => {
42+
expect(tally(["a", "a", "b", "c"])).toEqual({
43+
a: 2,
44+
b: 1,
45+
c: 1,
46+
});
47+
});
48+
49+
test("throws an error when input is not an array", () => {
50+
expect(() => tally("abc")).toThrow();
51+
});
52+
});

0 commit comments

Comments
 (0)