|
1 | 1 | const tally = require("./tally.js"); |
2 | 2 |
|
3 | | -/** |
4 | | - * tally array |
5 | | - * |
6 | | - * In this task, you'll need to implement a function called tally |
7 | | - * that will take a list of items and count the frequency of each item |
8 | | - * in an array |
9 | | - * |
10 | | - * For example: |
11 | | - * |
12 | | - * tally(['a']), target output: { a: 1 } |
13 | | - * tally(['a', 'a', 'a']), target output: { a: 3 } |
14 | | - * tally(['a', 'a', 'b', 'c']), target output: { a : 2, b: 1, c: 1 } |
15 | | - */ |
| 3 | +describe("tally()", () => { |
| 4 | + test("returns an empty object for an empty array", () => { |
| 5 | + expect(tally([])).toEqual({}); |
| 6 | + }); |
16 | 7 |
|
17 | | -// Acceptance criteria: |
| 8 | + test("counts a single item", () => { |
| 9 | + expect(tally(["a"])).toEqual({ a: 1 }); |
| 10 | + }); |
18 | 11 |
|
19 | | -// Given a function called tally |
20 | | -// When passed an array of items |
21 | | -// Then it should return an object containing the count for each unique item |
| 12 | + test("counts repeated items", () => { |
| 13 | + expect(tally(["a", "a", "a"])).toEqual({ a: 3 }); |
| 14 | + }); |
22 | 15 |
|
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"); |
| 16 | + test("counts multiple different items", () => { |
| 17 | + expect(tally(["a", "a", "b", "c"])).toEqual({ |
| 18 | + a: 2, |
| 19 | + b: 1, |
| 20 | + c: 1, |
| 21 | + }); |
| 22 | + }); |
27 | 23 |
|
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 |
| 24 | + test("throws an error for invalid input", () => { |
| 25 | + expect(() => tally("not-an-array")).toThrow(); |
| 26 | + }); |
| 27 | +}); |
0 commit comments