--- id: 5a23c84252665b21eecc8041 title: Sum of a series challengeType: 5 --- ## Description
Compute the nth term of a series, i.e. the sum of the n first terms of the corresponding sequence. Informally this value, or its limit when n tends to infinity, is also called the sum of the series, thus the title of this task. For this task, use: $S_n = \sum_{k=1}^n \frac{1}{k^2}$ and compute $S_{1000}$ This approximates the zeta function for S=2, whose exact value $\zeta(2) = {\pi^2\over 6}$ is the solution of the Basel problem. Write a function that take $a$ and $b$ as parameters and returns the sum of $a^{th}$ to $b^{th}$ members of the sequence.
## Instructions
## Tests
``` yml tests: - text: sum should be a function. testString: assert(typeof sum == 'function', 'sum should be a function.'); - text: sum(1, 100) should return a number. testString: assert(typeof sum(1, 100) == 'number', 'sum(1, 100) should return a number.'); - text: sum(1, 100) should return 1.6349839001848923. testString: assert.equal(sum(1, 100), 1.6349839001848923, 'sum(1, 100) should return 1.6349839001848923.'); - text: sum(33, 46) should return 0.009262256361481223. testString: assert.equal(sum(33, 46), 0.009262256361481223, 'sum(33, 46) should return 0.009262256361481223.'); - text: sum(21, 213) should return 0.044086990748706555. testString: assert.equal(sum(21, 213), 0.044086990748706555, 'sum(21, 213) should return 0.044086990748706555.'); - text: sum(11, 111) should return 0.08619778593108679. testString: assert.equal(sum(11, 111), 0.08619778593108679, 'sum(11, 111) should return 0.08619778593108679.'); - text: sum(1, 10) should return 1.5497677311665408. testString: assert.equal(sum(1, 10), 1.5497677311665408, 'sum(1, 10) should return 1.5497677311665408.'); ```
## Challenge Seed
```js function sum (a, b) { // Good luck! } ```
## Solution
```js function sum (a, b) { function fn(x) { return 1 / (x * x) } var s = 0; for (; a <= b; a++) s += fn(a); return s; } ```