Nested List Sum

Recursion Nested Array

Problem: Nested List Sum

Write a function that calculates the sum of all integers in a nested list. A nested list is an array (or list) that may contain integers as well as other nested lists. The function should use recursion to traverse and accumulate the sum of all numbers in the structure.

Example:

For the nested list:

[1, [2, 3], [4, [5]], 6]

The sum is: 1 + 2 + 3 + 4 + 5 + 6 = 21.

Notes:

  • The input will be a nested list that can contain both integers and other nested lists.
  • Use recursion to solve this problem.
  • As today (January 20) is a day to reflect on unity and collaboration (e.g., Martin Luther King Jr. Day in the US), think about how diverse elements can come together to create a larger whole.

Function Signature:

// Input: a nested list which can contain integers and nested lists
// Output: integer representing the sum of all numbers
function nestedListSum(nestedList) {}