1.7 KiB
1.7 KiB
title | id | challengeType | videoUrl | localeTitle |
---|---|---|---|---|
Accumulator factory | 594810f028c0303b75339ace | 5 | 蓄能器工厂 |
Description
创建一个带有单个(数字)参数的函数,并返回另一个作为累加器的函数。返回的累加器函数又接受一个数字参数,并返回到目前为止传递给该累加器的所有数值的总和(包括创建累加器时传递的初始值)。
规则:
不要使用全局变量。
暗示:
闭包可以保存外部状态。
Instructions
Tests
tests:
- text: <code>accumulator</code>是一个功能。
testString: 'assert(typeof accumulator === "function", "<code>accumulator</code> is a function.");'
- text: <code>accumulator(0)</code>应该返回一个函数。
testString: 'assert(typeof accumulator(0) === "function", "<code>accumulator(0)</code> should return a function.");'
- text: <code>accumulator(0)(2)</code>应该返回一个数字。
testString: 'assert(typeof accumulator(0)(2) === "number", "<code>accumulator(0)(2)</code> should return a number.");'
- text: '传递值3,-4,1.5和5应返回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
// solution required