Intermediate
Recursion through nested arrays
Write two recursive functions that work on arrays nested to any depth, like [1, [2, [3, [4]]], 5]:
sumNested(list) returns the sum of every number inside (an empty array gives 0).
flatten(list) returns a new flat array of every number in order, e.g. [1, 2, 3, 4, 5], without changing the array passed in.
What Check tests
sumNested([1, [2, [3, [4]]], 5])is 15- Empty arrays, even nested ones, sum to 0
- It handles deep nesting
flattenreturns all numbers in orderflattendoesn't change the array it was given
Stuck? One hint at a time
The solution unlocks after your first Check. Try first: that is where the learning happens.
Runs in your browser. Your code is saved here.
Press Run to see the page.
Console
Nothing printed yet.