3.1 KiB
3.1 KiB
id, challengeType, videoUrl, forumTopicId, localeTitle
id | challengeType | videoUrl | forumTopicId | localeTitle |
---|---|---|---|---|
5cfa3679138e7d9595b9d9d4 | 1 | https://www.freecodecamp.org/news/how-recursion-works-explained-with-flowcharts-and-a-video-de61f40cb7f9/ | 301175 | 使用递归代替循环 |
Description
0
到第 n
的元素乘积,使用 for
循环, 可以这样做:
function multiply(arr, n) {
var product = arr[0];
for (var i = 1; i <= n; i++) {
product *= arr[i];
}
return product;
}
下面是递归写法,注意代码里的 multiply(arr, n) == multiply(arr, n - 1) * arr[n]
。这意味着可以重写 multiply
以调用自身而无需依赖循环。
function multiply(arr, n) {
if (n <= 0) {
return arr[0];
} else {
return multiply(arr, n - 1) * arr[n];
}
}
递归版本的 multiply
详述如下。在 base case 里,也就是 n <= 0
时,返回结果,也就是 arr[0]
。在 n
比 0 大的情况里,函数会调用自身,参数 n 的值为 n - 1
。函数以相同的方式持续调用 multiply
,直到 n = 0
为止。所以,所有函数都可以返回,原始的 multiply
返回结果。
注意: 递归函数在没有函数调用时(在这个例子是,是当 n <= 0
时)必需有一个跳出结构,否则永远不会执行完毕。
Instructions
写一个递归函数,sum(arr, n)
,返回递归调用数组 arr
从第 0
个到第 n
个元素和。
Tests
tests:
- text: <code>sum([1], 0)</code> 应该返回 1 。
testString: assert.equal(sum([1], 0), 1);
- text: <code>sum([2, 3, 4], 1)</code> 应该返回 5 。
testString: assert.equal(sum([2, 3, 4], 1), 5);
- text: 代码不应该包含任何形式的循环(<code>for</code> 或者 <code>while</code> 或者高阶函数比如 <code>forEach</code>,<code>map</code>,<code>filter</code>,或者 <code>reduce</code>)。
testString: assert(!removeJSComments(code).match(/for|while|forEach|map|filter|reduce/g));
- text: 应该使用递归来解决这个问题。
testString: assert(removeJSComments(sum.toString()).match(/sum\(.*\)/g).length > 1);
Challenge Seed
function sum(arr, n) {
// Only change code below this line
// Only change code above this line
}
After Test
const removeJSComments = str => str.replace(/\/\*[\s\S]*?\*\/|\/\/.*$/gm, '');
Solution
function sum(arr, n) {
// Only change code below this line
if(n <= 0) {
return arr[0];
} else {
return sum(arr, n - 1) + arr[n];
}
// Only change code above this line
}