|
1 | | -/* Sum the numbers in an array |
2 | | -
|
3 | | -In this kata, you will need to implement a function that sums the numerical elements of an array |
4 | | -
|
5 | | -E.g. sum([10, 20, 30]), target output: 60 |
6 | | -E.g. sum(['hey', 10, 'hi', 60, 10]), target output: 80 (ignore any non-numerical elements) |
7 | | -*/ |
8 | | - |
9 | 1 | const sum = require("./sum.js"); |
10 | 2 |
|
11 | | -// Acceptance Criteria: |
12 | | - |
13 | | -// Given an empty array |
14 | | -// When passed to the sum function |
15 | | -// Then it should return 0 |
16 | | -test.todo("given an empty array, returns 0") |
17 | | - |
18 | | -// Given an array with just one number |
19 | | -// When passed to the sum function |
20 | | -// Then it should return that number |
21 | | - |
22 | | -// Given an array containing negative numbers |
23 | | -// When passed to the sum function |
24 | | -// Then it should still return the correct total sum |
25 | | - |
26 | | -// Given an array with decimal/float numbers |
27 | | -// When passed to the sum function |
28 | | -// Then it should return the correct total sum |
29 | | - |
30 | | -// Given an array containing non-number values |
31 | | -// When passed to the sum function |
32 | | -// Then it should ignore the non-numerical values and return the sum of the numerical elements |
33 | | - |
34 | | -// Given an array with only non-number values |
35 | | -// When passed to the sum function |
36 | | -// Then it should return the least surprising value given how it behaves for all other inputs |
| 3 | +describe("sum", () => { |
| 4 | + // Given an empty array |
| 5 | + // When passed to the sum function |
| 6 | + // Then it should return 0 |
| 7 | + test("given an empty array, returns 0", () => { |
| 8 | + expect(sum([])).toBe(0); |
| 9 | + }); |
| 10 | + |
| 11 | + // Given an array with just one number |
| 12 | + // When passed to the sum function |
| 13 | + // Then it should return that number |
| 14 | + test("given an array with one number, returns that number", () => { |
| 15 | + expect(sum([5])).toBe(5); |
| 16 | + }); |
| 17 | + |
| 18 | + // Given an array containing negative numbers |
| 19 | + // When passed to the sum function |
| 20 | + // Then it should still return the correct total sum |
| 21 | + test("sums negative numbers correctly", () => { |
| 22 | + expect(sum([-5, -10, 15])).toBe(0); |
| 23 | + }); |
| 24 | + |
| 25 | + // Given an array with decimal/float numbers |
| 26 | + // When passed to the sum function |
| 27 | + // Then it should return the correct total sum |
| 28 | + test("sums decimal numbers correctly", () => { |
| 29 | + expect(sum([1.5, 2.5, 3.5])).toBe(7.5); |
| 30 | + }); |
| 31 | + |
| 32 | + // Given an array containing non-number values |
| 33 | + // When passed to the sum function |
| 34 | + // Then it should ignore the non-numerical values |
| 35 | + test("ignores non-number values", () => { |
| 36 | + expect(sum(["hey", 10, "hi", 60, 10])).toBe(80); |
| 37 | + }); |
| 38 | + |
| 39 | + // Given an array with only non-number values |
| 40 | + // When passed to the sum function |
| 41 | + // Then it should return the least surprising value |
| 42 | + test("returns 0 when array contains only non-number values", () => { |
| 43 | + expect(sum(["a", "b", "c"])).toBe(0); |
| 44 | + }); |
| 45 | +}); |
0 commit comments