Skip to content

Instantly share code, notes, and snippets.

@hoorayimhelping
Last active October 28, 2022 14:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hoorayimhelping/f6d9537f7d8f01c8c8940dadf7e5d33b to your computer and use it in GitHub Desktop.
Save hoorayimhelping/f6d9537f7d8f01c8c8940dadf7e5d33b to your computer and use it in GitHub Desktop.

Write a function that takes an array [1, 2, [3, 4, [5], 6], 7] and returns the sum of all elements where each element is multiplied by its depth. In this example:

(1 * 1) + (2 * 1) + (3 * 2) + (4 * 2) + (5 * 3) + (6 * 2) + (7 * 1) = 51

hint: recursion makes this simpler to implement

sample input:

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

output: 51

Other input:

[5, 15, [3], [4], [4, 7, 15, [25, 35], 15], 0]

output: 296
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment