Skip to content

Instantly share code, notes, and snippets.

@ahafidi
Created December 28, 2021 17:23
Show Gist options
  • Save ahafidi/431a693e58f81904d88d7cdebb007a98 to your computer and use it in GitHub Desktop.
Save ahafidi/431a693e58f81904d88d7cdebb007a98 to your computer and use it in GitHub Desktop.
const arrayCalculation = (specialArray, depth = 1) => {
const r = []
specialArray.forEach((e) => {
if (Number.isInteger(e))
r.push(e)
if (Array.isArray(e))
r.push(arrayCalculation(e, depth + 1))
})
return depth * r.reduce((acc, cv) => acc + cv, 0)
}
// [2, [4, 6]] => 2 + 2 * (4 + 6) = 22
// [5, [[7], 3] => 5 + 2 * (3 * 7 + 3) = 53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment