MCPcopy Create free account
hub / github.com/btholt/algorithms-exercises / nestedAdd

Function nestedAdd

specs/recursion/nested-arrays.solution.test.js:12–23  ·  view source on GitHub ↗
(array)

Source from the content-addressed store, hash-verified

10 */
11
12function nestedAdd(array) {
13 let sum = 0;
14 for (let i = 0; i < array.length; i++) {
15 const current = array[i];
16 if (Array.isArray(current)) {
17 sum += nestedAdd(current);
18 } else {
19 sum += current;
20 }
21 }
22 return sum;
23}
24
25test("nested arrays addition", () => {
26 expect(nestedAdd([1, 2, 3])).toEqual(6);

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected