1.7 KiB
1.7 KiB
title, id, challengeType
title | id | challengeType |
---|---|---|
Accumulator factory | 594810f028c0303b75339ace | 5 |
Description
Create a function that takes a single (numeric) argument and returns another function that is an accumulator. The returned accumulator function in turn also takes a single numeric argument, and returns the sum of all the numeric values passed in so far to that accumulator (including the initial value passed when the accumulator was created).
Rules:
Do not use global variables.
Hint:
Closures save outer state.
Instructions
Tests
tests:
- text: <code>accumulator</code> is a function.
testString: 'assert(typeof accumulator === "function", "<code>accumulator</code> is a function.");'
- text: <code>accumulator(0)</code> should return a function.
testString: 'assert(typeof accumulator(0) === "function", "<code>accumulator(0)</code> should return a function.");'
- text: <code>accumulator(0)(2)</code> should return a number.
testString: 'assert(typeof accumulator(0)(2) === "number", "<code>accumulator(0)(2)</code> should return a number.");'
- text: 'Passing in the values 3, -4, 1.5, and 5 should return 5.5.'
testString: 'assert(testFn(5) === 5.5, "Passing in the values 3, -4, 1.5, and 5 should return 5.5.");'
Challenge Seed
function accumulator (sum) {
// Good luck!
}
After Test
console.info('after the test');
Solution
function accumulator (sum) {
return function (n) {
return sum += n;
};
}