Skip to content

Commit 5ebaa3d

Browse files
implemented the tally fiunction and added some jest test for the function
1 parent 0c16c1d commit 5ebaa3d

4 files changed

Lines changed: 62 additions & 27 deletions

File tree

‎Sprint-2/implement/lookup.js‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
function createLookup(countrycurrency) {
2-
const lookup = {};
2+
const lookup = {}; // lookup object is created empty to store the new keys and values mapped from the function
33
countrycurrency.map(function ([country, currency]) {
4+
// maps every keys and values passed as argument in countrycurrency and stores to the variable names of country and currency
45
console.log(`${country}: ${currency}`);
5-
lookup[country] = currency;
6+
lookup[country] = currency; // this lookup object is then used to store the already mapped keys and values of the countrycurrency
67
});
78
return lookup;
89
}

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

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,30 @@
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 object for multiple codes", () => {
4+
const result = createLookup([
5+
["US", "USD"],
6+
["UAE", "AED"],
7+
["ERI", "ERN"],
8+
]);
9+
10+
expect(result).toEqual({
11+
US: "USD",
12+
UAE: "AED",
13+
ERI: "ERN",
14+
});
15+
});
16+
17+
test("prints country and currency vertically", () => {
18+
console.log = jest.fn(); //mocks the console.log which is used to print out only returned values, but since this test only checks the printed output but not returned values, we do mock the console.log to check the printed output
19+
20+
createLookup([
21+
["US", "USD"],
22+
["CA", "CAD"],
23+
]);
24+
25+
expect(console.log).toHaveBeenCalledWith("US: USD");
26+
expect(console.log).toHaveBeenCalledWith("CA: CAD");
27+
});
428

529
/*
630

‎Sprint-2/implement/tally.js‎

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,14 @@
11
function tally(arr) {
2-
let counts = {};
2+
if (!Array.isArray(arr)) throw new TypeError("Input must be an array");
3+
const counts = Object.create(null);
34
if (arr.length === 0) {
4-
return {};
5+
return counts;
56
}
6-
if (arr !== /^[0-9]A-za-z$/g) {
7-
throw new Error("Please Enter Valid Input!");
8-
}
9-
107
for (const item of arr) {
11-
if (!/^[A-Za-z0-9]$/.test(item)) {
8+
if (typeof item !== "string" || !/^[A-Za-z0-9]+$/.test(item)) {
129
throw new Error("Please Enter Valid Input!");
1310
}
14-
if (counts.hasOwnProperty(item)) {
15-
counts[item] += 1;
16-
} else {
17-
counts[item] = 1;
18-
}
11+
counts[item] = (counts[item] ?? 0) + 1;
1912
}
2013
return counts;
2114
}

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

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,33 @@ 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+
describe("tally", () => {
23+
test("Given an array of items, returns counts for each unique item", () => {
24+
const input = ["apple", "banana", "apple", "orange", "banana", "apple"];
25+
const expected = { apple: 3, banana: 2, orange: 1 };
26+
expect(tally(input)).toEqual(expected);
27+
});
28+
// Given an empty array
29+
// When passed to tally
30+
// Then it should return an empty object
31+
test("tally on an empty array returns an empty object", () => {
32+
expect([]).toEqual({});
33+
});
2234

23-
// Given an empty array
24-
// When passed to tally
25-
// Then it should return an empty object
26-
test.todo("tally on an empty array returns an empty object");
27-
28-
// Given an array with duplicate items
29-
// When passed to tally
30-
// Then it should return counts for each unique item
31-
32-
// Given an invalid input like a string
33-
// When passed to tally
34-
// Then it should throw an error
35+
// Given an array with duplicate items
36+
// When passed to tally
37+
// Then it should return counts for each unique item
38+
test("Given an array with duplicate items, returns counts for each unique item", () => {
39+
const input = ["x", "x", "y", "x", "y"];
40+
const expected = { x: 3, y: 2 };
41+
expect(tally(input)).toEqual(expected);
42+
});
43+
// Given an invalid input like a string
44+
// When passed to tally
45+
// Then it should throw an error
46+
test("Given an array containing an invalid item, throws an Error", () => {
47+
expect(() => tally(["valid", "hello world"])).toThrow(
48+
"Please Enter Valid Input!"
49+
);
50+
});
51+
});

0 commit comments

Comments
 (0)