DEV Community

chandra penugonda
chandra penugonda

Posted on

3 3

Compute the sum of an array of integers using Recursion

Problem statement:

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

var sum = function(array) {
    // start here
};
Enter fullscreen mode Exit fullscreen mode

solution:

var sum = function (array) {
  if (array.length === 1) return array[0];
  return array[0] + sum(array.slice(1));
};
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay