* feat(tools): add seed/solution restore script * chore(curriculum): remove empty sections' markers * chore(curriculum): add seed + solution to Chinese * chore: remove old formatter * fix: update getChallenges parse translated challenges separately, without reference to the source * chore(curriculum): add dashedName to English * chore(curriculum): add dashedName to Chinese * refactor: remove unused challenge property 'name' * fix: relax dashedName requirement * fix: stray tag Remove stray `pre` tag from challenge file. Signed-off-by: nhcarrigan <nhcarrigan@gmail.com> Co-authored-by: nhcarrigan <nhcarrigan@gmail.com>
1.5 KiB
id, title, challengeType, forumTopicId, dashedName
id | title | challengeType | forumTopicId | dashedName |
---|---|---|---|---|
594810f028c0303b75339ace | Accumulator factory | 5 | 302222 | accumulator-factory |
--description--
A problem posed by Paul Graham is that of creating a function that takes a single (numeric) argument and which 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).
--instructions--
Create a function that takes a number n
and generates accumulator functions that return the sum of every number ever passed to them.
Rules:
Do not use global variables.
Hint:
Closures save outer state.
--hints--
accumulator
should be a function.
assert(typeof accumulator === 'function');
accumulator(0)
should return a function.
assert(typeof accumulator(0) === 'function');
accumulator(0)(2)
should return a number.
assert(typeof accumulator(0)(2) === 'number');
Passing in the values 3, -4, 1.5, and 5 should return 5.5.
assert(testFn(5) === 5.5);
--seed--
--after-user-code--
const testFn = typeof accumulator(3) === 'function' && accumulator(3);
if (testFn) {
testFn(-4);
testFn(1.5);
}
--seed-contents--
function accumulator(sum) {
}
--solutions--
function accumulator(sum) {
return function(n) {
return sum += n;
};
}