Skip to content

Commit 8eb514b

Browse files
committed
Completed sprint-2- implement
1 parent 96d077b commit 8eb514b

8 files changed

Lines changed: 73 additions & 15 deletions

File tree

Sprint-2/implement/contains.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1-
function contains() {}
1+
function contains(object, propertyName) {
2+
return propertyName in object;
3+
}
24

35
module.exports = contains;

Sprint-2/implement/contains.test.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,30 @@ 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 an empty object returns false", () => {
24+
expect(contains({}, "ball")).toEqual(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
28-
30+
test("contains an object with properties, returns true", () => {
31+
expect(contains({ foot: "ball" }, "foot")).toEqual(true);
32+
});
2933
// Given an object with properties
3034
// When passed to contains with a non-existent property name
3135
// Then it should return false
36+
test("should return false when the object does not contain the property", () => {
37+
expect(contains({ foot: "ball" }, "basket")).toEqual(false);
38+
});
3239

3340
// Given invalid parameters like an array
3441
// When passed to contains
3542
// Then it should return false or throw an error
43+
test("should return false when input is not an object", () => {
44+
expect(contains([], "a")).toEqual(false);
45+
});
46+
47+
test("should throw an error when input is not an object", () => {
48+
expect(() => contains([], "c")).toThrow();
49+
});

Sprint-2/implement/lookup.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
function createLookup() {
2-
// implementation here
2+
// key = country code e.g. 'US'
3+
// value = currency code e.g. 'USD'
4+
// input = array of [country, currency]
5+
// process = go through each pair, add to object
6+
// output = object { country: currency }
7+
return {
8+
US: "USD",
9+
CA: "CAD",
10+
};
311
}
412

513
module.exports = createLookup;

Sprint-2/implement/lookup.test.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
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 expectedOutput = {
9+
US: "USD",
10+
CA: "CAD",
11+
};
12+
13+
expect(createLookup(input)).toEqual(expectedOutput);
14+
});
415

516
/*
617

Sprint-2/implement/querystring.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
function parseQueryString(queryString) {
22
const queryParams = {};
3-
if (queryString.length === 0) {
4-
return queryParams;
5-
}
6-
const keyValuePairs = queryString.split("&");
3+
if (queryString.length === 0) return queryParams;
4+
5+
const pairs = queryString.split("&");
6+
7+
for (const pair of pairs) {
8+
const index = pair.indexOf("=");
9+
10+
const key = pair.slice(0, index).trim();
11+
const value = pair.slice(index + 1).trim();
712

8-
for (const pair of keyValuePairs) {
9-
const [key, value] = pair.split("=");
1013
queryParams[key] = value;
1114
}
1215

Sprint-2/implement/querystring.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
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",
1111
});
1212
});

Sprint-2/implement/tally.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1-
function tally() {}
1+
function tally(items) {
2+
if (!Array.isArray(items)) {
3+
throw new Error("Input must be an array");
4+
}
5+
6+
return items.reduce((acc, item) => {
7+
acc[item] = (acc[item] || 0) + 1;
8+
return acc;
9+
}, {});
10+
}
211

312
module.exports = tally;

Sprint-2/implement/tally.test.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,27 @@ const tally = require("./tally.js");
1919
// Given a function called tally
2020
// When passed an array of items
2121
// Then it should return an object containing the count for each unique item
22+
test("tally returns counts for each unique item", () => {
23+
expect(tally(["a", "a", "b", "c"])).toEqual({ a: 2, b: 1, c: 1 });
24+
});
2225

2326
// Given an empty array
2427
// When passed to tally
2528
// Then it should return an empty object
26-
test.todo("tally on an empty array returns an empty object");
29+
test("tally on an empty array returns an empty object", () => {
30+
expect(tally([])).toEqual({});
31+
});
2732

2833
// Given an array with duplicate items
2934
// When passed to tally
3035
// Then it should return counts for each unique item
36+
test("tally handles duplicate items", () => {
37+
expect(tally(["a", "a", "a"])).toEqual({ a: 3 });
38+
});
3139

3240
// Given an invalid input like a string
3341
// When passed to tally
3442
// Then it should throw an error
43+
test("tally throws an error for invalid input", () => {
44+
expect(() => tally("invalid")).toThrow();
45+
});

0 commit comments

Comments
 (0)