Skip to content

Commit b2f3129

Browse files
committed
chore: Solutions for Sprint 2
1 parent c64ea96 commit b2f3129

6 files changed

Lines changed: 80 additions & 6 deletions

File tree

‎Sprint-2/implement/contains.js‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1-
function contains() {}
1+
// Explanation:
2+
3+
// !Array.isArray(object) checks if the first parameter is an array, the exclamation mark flips the condition result
4+
// meaning if we pass an object like we are supposed to, the condition will be true instead of false
5+
// (&&) is used to join two conditions, so both need to be true for true to be returned by the function.
6+
// object.hasOwnProperty(key) just checks if the key exists in the object.
7+
8+
function contains(object, key) {
9+
return !Array.isArray(object) && object.hasOwnProperty(key);
10+
}
211

312
module.exports = contains;

‎Sprint-2/implement/contains.test.js‎

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
const contains = require("./contains.js");
2-
31
/*
42
Implement a function called contains that checks an object contains a
53
particular property
@@ -32,3 +30,23 @@ as the object doesn't contains a key of 'c'
3230
// Given invalid parameters like arrays
3331
// When passed to contains
3432
// Then it should return false or throw an error
33+
34+
const contains = require("./contains.js");
35+
36+
describe("contains", () => {
37+
it("returns true when the object contains the key specified", () => {
38+
expect(contains({ a: 1, b: 2 }, "a")).toBeTruthy();
39+
});
40+
41+
it("returns false when the object does not contain the key specified", () => {
42+
expect(contains({ a: 1, b: 2 }, "c")).toBeFalsy();
43+
});
44+
45+
it("returns false when the object is empty", () => {
46+
expect(contains({}, "c")).toBeFalsy();
47+
});
48+
49+
it("returns false when an array is used rather than an object", () => {
50+
expect(contains(["c"], "c")).toBeFalsy();
51+
});
52+
});

‎Sprint-2/implement/lookup.js‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1-
function createLookup() {
2-
// implementation here
1+
// Explanation:
2+
3+
// Reduce is used to aggregate values, in this case, acc is an object which we are using to store
4+
// the countryCode and currencyCode key value pairs
5+
6+
function createLookup(currencyCodes) {
7+
return currencyCodes.reduce((acc, [countryCode, currencyCode]) => {
8+
acc[countryCode] = currencyCode;
9+
return acc;
10+
}, {});
311
}
12+
13+
module.exports = createLookup;

‎Sprint-2/implement/lookup.test.js‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// ======= Test suite is provided below... =======
22

3+
const createLookup = require("./lookup");
4+
35
test("converts a single pair of currency codes", () => {
46
expect(createLookup([["GB", "GBP"]])).toEqual({
57
GB: "GBP",
@@ -9,7 +11,12 @@ test("converts a single pair of currency codes", () => {
911
});
1012
});
1113

12-
test.todo("creates a country currency code lookup for multiple codes");
14+
test("converts a multiple pairs of currency codes", () => {
15+
expect(createLookup([["GB", "GBP"], ["DE", "EUR"]])).toEqual({
16+
GB: "GBP",
17+
DE: "EUR",
18+
});
19+
});
1320

1421
/*
1522

‎Sprint-2/implement/tally.js‎

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

‎Sprint-2/implement/tally.test.js‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,21 @@
2929
// Given an invalid input like a string
3030
// When passed to tally
3131
// Then it should throw an error
32+
33+
const tally = require("./tally");
34+
35+
describe("tally", () => {
36+
it("returns the correct tally for each item in the array", () => {
37+
expect(tally(["a"])).toEqual({ a: 1 });
38+
expect(tally(["a", "a", "b", "b", "b", "c"])).toEqual({ a: 2, b: 3, c: 1 });
39+
});
40+
41+
it("returns an empty object when the array is empty", () => {
42+
expect(tally([])).toEqual({});
43+
});
44+
45+
it("throws an error when the input is not an array", () => {
46+
expect(() => tally("test")).toThrow("Invalid input: input must be an array");
47+
expect(() => tally(123)).toThrow("Invalid input: input must be an array");
48+
});
49+
});

0 commit comments

Comments
 (0)