Skip to content

Commit e431148

Browse files
committed
tally function and testing it
1 parent 47c979a commit e431148

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

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(arr) {
2+
let object = {};
3+
if (!Array.isArray(arr)) {
4+
throw new Error("This is not an array");
5+
} else if (arr.length < 1) {
6+
return {};
7+
} else {
8+
for (let i = 0; i < arr.length; i++) {
9+
const some_value = arr[i];
10+
object[some_value] = (object[some_value] || 0) + 1;
11+
}
12+
return object;
13+
}
14+
}
15+
16+
const result = tally([1, 2, 3, 1, 2, 3, 6]);
17+
console.log(result);
218

319
module.exports = tally;

Sprint-2/implement/tally.test.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,43 @@ 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("edge cases of the tally function", () => {
23+
test("When passed an array of items as a parameter into tally", () => {
24+
const currentOutput = tally([1, 2, 4, 5, 6]);
25+
const targetOutput = { 1: 1, 2: 1, 4: 1, 5: 1, 6: 1 };
26+
expect(currentOutput).toEqual(targetOutput);
27+
});
28+
});
2229

2330
// Given an empty array
2431
// When passed to tally
2532
// Then it should return an empty object
26-
test.todo("tally on an empty array returns an empty object");
33+
describe("edge cases of the tally function", () => {
34+
test("tally on an empty array returns an empty object", () => {
35+
const currentOutput = tally([]);
36+
const targetOutput = {};
37+
expect(currentOutput).toEqual(targetOutput);
38+
});
39+
});
2740

2841
// Given an array with duplicate items
2942
// When passed to tally
3043
// Then it should return counts for each unique item
44+
describe("edge cases of the tally function", () => {
45+
test("When passed an array with duplicate items of items as a parameter into tally", () => {
46+
const currentOutput = tally([1, 2, 2, 6, 6, 6, 1]);
47+
const targetOutput = { 1: 2, 2: 2, 6: 3 };
48+
expect(currentOutput).toEqual(targetOutput);
49+
});
50+
});
3151

3252
// Given an invalid input like a string
3353
// When passed to tally
3454
// Then it should throw an error
55+
describe("edge cases of the tally function", () => {
56+
test("tally gets not an array then it trow an error", () => {
57+
// const currentOutput = tally("string");
58+
// const targetOutput = Error;
59+
expect(() => tally("string")).toThrow("This is not an array");
60+
});
61+
});

0 commit comments

Comments
 (0)