简体   繁体   English

JS中的递归函数问题

[英]Recursive function issue in JS

So I need to solve this problem STRICTLY using recursion 所以我需要使用递归来解决这个问题STRICTLY

 // 2. Compute the sum of an array of integers. // sum([1,2,3,4,5,6]); // 21 

And then I'm testing this solution in PythonLive 然后我在PythonLive中测试这个解决方案

 var sum = function(array) { if(array.length===0){ return array } return array.slice(0,array.length)+sum(array.pop()) }; sum([1,2,3,4,5,6]); 

Then at step 6 it says "TypeError: array.slice is not a function" 然后在第6步它说“TypeError:array.slice不是一个函数”

I don't understand why if it already worked taking 6 off the array and returning the remaining array... 我不明白为什么如果它已经工作从数组中取出6并返回剩余的数组...

Can someone explain me what am I doing wrong please? 有人可以解释一下我做错了吗?

thanks! 谢谢! :) :)

If you look at the return values you will see that you are always returning an array. 如果查看返回值,您将看到始终返回一个数组。 This can't be right when you want a number as a final result. 当你想要一个数字作为最终结果时,这是不对的。 When array.length === 0 you can safely return 0 because that's the same of an empty array. array.length === 0你可以安全地返回0,因为它与空数组相同。 That's your edge condition. 这是你的边缘条件。 After that you just want the sum of one element plus the rest. 之后你只需要一个元素和其余元素的总和。

You can also just return the array length when it's zero making for a very succinct solution. 你也可以在它为零时返回数组长度,以获得非常简洁的解决方案。 && shortcircuits returning the left element if it's false (like 0) otherwise the second: && shortcircuits返回左侧元素,如果它是假的(如0),否则第二个:

 var sum = (array) => array.length && array.pop() + sum(array) console.log(sum([1,2,3,4,5,6])); 

If you prefer slice you could also this, which is basically the same: 如果您更喜欢slice您也可以这样,这基本上是相同的:

 var sum = (array) => array.length && array[0] + sum(array.slice(1)) console.log(sum([1, 2, 3, 4, 5, 6])); 

The issue with your code is that you are processing the values the wrong way around, it should be 您的代码的问题在于您正在以错误的方式处理值,它应该是

return sum(array.slice(0,array.length-1)) + array.pop();

In fact since array.pop() removes the element, you can just do it this way around: 事实上,因为array.pop()删除了元素,你可以这样做:

return array.pop() + sum(array);

You also need to return 0 when array.length===0 , otherwise the sum will fail. array.length===0 ,您还需要返回0 ,否则总和将失败。

if (array.length===0) return 0;

However it's much simpler just do this with reduce : 然而,通过reduce这样做更简单:

 let arr = [1,2,3,4,5,6]; console.log(arr.reduce((t, v) => { return t + v; }, 0)); 

Recursive sum function: 递归和函数:

const sum = list => {
  if (!list.length) return 0;
  return list.pop() + sum(list);
};

Because .pop mutates the array, you don't need to use slice. 因为.pop改变数组,所以不需要使用切片。 For a non-destructive version (doesn't alter the original array): 对于非破坏性版本(不会更改原始数组):

const sum = ([first, ...rest]) => {
  if (first === undefined) return 0;
  return first + sum(rest);
};

Another encoding using an explicit empty and pure expressions 使用显式empty和纯表达式的另一种编码

 const empty = x => x === empty const sum = ([ x = empty, ...rest ]) => empty (x) ? 0 : x + sum (rest) console.log ( sum ([ 1, 2, 3, 4, 5 ]) // 15 , sum ([]) // 0 ) 

Your other question that asks how to sum a nested array was put on-hold for reasons I don't understand. 你问的如何对嵌套数组求和的另一个问题被搁置了,原因我不明白。 You can adapt the above implementation to support input of nested arrays 您可以调整上述实现以支持嵌套数组的输入

 const empty = x => x === empty const sum = ([ x = empty, ...rest ]) => empty (x) ? 0 : Array.isArray (x) ? sum (x) + sum (rest) : x + sum (rest) console.log ( sum ([ 1, [ 2, [ 3, 4 ], 5 ]]) // 15 , sum ([ 1, 2, 3, 4, 5 ]) // 15 , sum ([[[]]]) // 0 , sum ([]) // 0 ) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM