Skip to content

Commit a342ca2

Browse files
committed
sum of the first nth term of series
1 parent 9b33f7a commit a342ca2

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
function SeriesSum(n) {
2+
/// ES6
3+
return [...Array(n).keys()].map((_, idx) => 1 + idx * 3).reduce((acc, val) => (acc += 1 / val, acc), 0).toFixed(2);
4+
5+
/// for loop
6+
let result = 0;
7+
for(let i = 0, j = 1; i < n; i++, j+=3) {
8+
result += (1 / j);
9+
}
10+
return result.toFixed(2);
11+
}
12+
13+
module.exports = SeriesSum;
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const SeriesSum = require('./main');
2+
3+
/// when the parameter is 0.
4+
5+
describe('When the parameter is 0', () => {
6+
test('if 0 returns 0.00 as string', () => {
7+
expect(SeriesSum(0)).toBe('0.00');
8+
});
9+
});
10+
11+
describe('Sum series test', () => {
12+
test('if 1 returns 1.00 as string', () => {
13+
expect(SeriesSum(1)).toBe('1.00');
14+
});
15+
test('if 2 returns 1.25 as string', () => {
16+
expect(SeriesSum(2)).toBe('1.25');
17+
});
18+
test('if 3 returns 1.39 as string', () => {
19+
expect(SeriesSum(3)).toBe('1.39');
20+
});
21+
test('if 4 returns 1.49 as string', () => {
22+
expect(SeriesSum(4)).toBe('1.49');
23+
});
24+
});

0 commit comments

Comments
 (0)