2.8 KiB
2.8 KiB
id | title | challengeType |
---|---|---|
5a23c84252665b21eecc8041 | Sum of a series | 5 |
Description
Instructions
Tests
tests:
- text: <code>sum</code> should be a function.
testString: assert(typeof sum == 'function', '<code>sum</code> should be a function.');
- text: <code>sum(1, 100)</code> should return a number.
testString: assert(typeof sum(1, 100) == 'number', '<code>sum(1, 100)</code> should return a number.');
- text: <code>sum(1, 100)</code> should return <code>1.6349839001848923</code>.
testString: assert.equal(sum(1, 100), 1.6349839001848923, '<code>sum(1, 100)</code> should return <code>1.6349839001848923</code>.');
- text: <code>sum(33, 46)</code> should return <code>0.009262256361481223</code>.
testString: assert.equal(sum(33, 46), 0.009262256361481223, '<code>sum(33, 46)</code> should return <code>0.009262256361481223</code>.');
- text: <code>sum(21, 213)</code> should return <code>0.044086990748706555</code>.
testString: assert.equal(sum(21, 213), 0.044086990748706555, '<code>sum(21, 213)</code> should return <code>0.044086990748706555</code>.');
- text: <code>sum(11, 111)</code> should return <code>0.08619778593108679</code>.
testString: assert.equal(sum(11, 111), 0.08619778593108679, '<code>sum(11, 111)</code> should return <code>0.08619778593108679</code>.');
- text: <code>sum(1, 10)</code> should return <code>1.5497677311665408</code>.
testString: assert.equal(sum(1, 10), 1.5497677311665408, '<code>sum(1, 10)</code> should return <code>1.5497677311665408</code>.');
Challenge Seed
function sum (a, b) {
// Good luck!
}
Solution
function sum (a, b) {
function fn(x) {
return 1 / (x * x)
}
var s = 0;
for (; a <= b; a++) s += fn(a);
return s;
}