Skip to content

Commit 1c843d4

Browse files
committed
Work on implement
1 parent ec33971 commit 1c843d4

6 files changed

Lines changed: 95 additions & 7 deletions

File tree

‎Sprint-2/implement/contains.js‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,21 @@
1-
function contains() {}
1+
function contains(toCheck, input) {
2+
if (
3+
typeof toCheck !== "object" ||
4+
Array.isArray(toCheck) ||
5+
toCheck === null
6+
) {
7+
return false;
8+
}
9+
10+
for (const i in toCheck) {
11+
if (i === input) {
12+
return true;
13+
}
14+
}
15+
16+
return false;
17+
}
18+
19+
console.log(contains({ a: 1, b: 2 }, "a"));
220

321
module.exports = contains;

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,17 @@ as the object doesn't contains a key of 'c'
1717
// When passed an object and a property name
1818
// Then it should return true if the object contains the property, false otherwise
1919

20+
test(`Should return true if object contains the property`, () => {
21+
expect(contains({ a: 1, b: 2 }, "a")).toEqual(true);
22+
expect(contains({ b: 2 }, "a")).toEqual(false);
23+
});
24+
2025
// Given an empty object
2126
// When passed to contains
2227
// Then it should return false
23-
test.todo("contains on empty object returns false");
28+
test(`Should return false if object is empty`, () => {
29+
expect(contains({}, "a")).toEqual(false);
30+
});
2431

2532
// Given an object with properties
2633
// When passed to contains with an existing property name
@@ -33,3 +40,7 @@ test.todo("contains on empty object returns false");
3340
// Given invalid parameters like an array
3441
// When passed to contains
3542
// Then it should return false or throw an error
43+
44+
test(`Should return false if given wron type`, () => {
45+
expect(contains(["a", "b"], "0")).toEqual(false);
46+
});

‎Sprint-2/implement/lookup.js‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,22 @@
1-
function createLookup() {
1+
function createLookup(array) {
22
// implementation here
3+
4+
if (!Array.isArray(array)) {
5+
throw new Error("Wrong input type, expected array");
6+
}
7+
8+
if (array.length === 0) {
9+
throw new Error("Array is empty");
10+
}
11+
12+
let output = {};
13+
for (const i of array) {
14+
const key = i[0];
15+
const value = i[1];
16+
17+
output[key] = value;
18+
}
19+
return output;
320
}
421

522
module.exports = createLookup;

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

Lines changed: 15 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,18 @@ It should return:
3331
'CA': 'CAD'
3432
}
3533
*/
34+
35+
test(`Should return an object with correct keys`, () => {
36+
expect(
37+
createLookup([
38+
["US", "USD"],
39+
["CA", "CAD"],
40+
])
41+
).toEqual({ US: "USD", CA: "CAD" });
42+
});
43+
44+
test(`Should throw an error if no input given`, () => {
45+
expect(() => {
46+
createLookup();
47+
}).toThrow();
48+
});

‎Sprint-2/implement/tally.js‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,21 @@
1-
function tally() {}
1+
function tally(input) {
2+
let out = {};
3+
4+
if (!Array.isArray(input)) {
5+
throw new Error(
6+
`Wrong input type, expected an array but got ${typeof input}`
7+
);
8+
}
9+
10+
for (const i of input) {
11+
if (out[i] === undefined) {
12+
out[i] = 1;
13+
} else if (out[i].hasOwnProperty(i)) {
14+
out[i] = out[i] + 1;
15+
}
16+
}
17+
18+
return out;
19+
}
220

321
module.exports = tally;

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,23 @@ const tally = require("./tally.js");
2323
// Given an empty array
2424
// When passed to tally
2525
// Then it should return an empty object
26-
test.todo("tally on an empty array returns an empty object");
26+
test(`Should an empty object when empty array is passed`, () => {
27+
expect(tally([])).toEqual({});
28+
});
2729

2830
// Given an array with duplicate items
2931
// When passed to tally
3032
// Then it should return counts for each unique item
33+
test(`Should return counts for each unique item`, () => {
34+
expect(tally(["a", "a", "b"])).toEqual({ a: 2, b: 1 });
35+
expect(tally(["toString", "a", "b"])).toEqual({ toString: 1, a: 1, b: 1 });
36+
});
3137

3238
// Given an invalid input like a string
3339
// When passed to tally
3440
// Then it should throw an error
41+
test(`Should return an error when given wrong input type`, () => {
42+
expect(() => {
43+
tally("hello");
44+
}).toThrow();
45+
});

0 commit comments

Comments
 (0)