Problem 29

Sum even numbers in nested objects Recursion Challenge

Given an object that may contain nested objects, return the sum of all even numbers found at any depth. Ignore odd numbers and non-number values.

Function Signature

nestedEvenSum(obj)

Parameters

  • obj — an object whose values may be numbers, strings, or nested objects.

Output

Return a number representing the sum of all even-valued numbers in the object tree.

Constraints

  • The function must use recursion.
  • The function should accept exactly one argument.

Examples

// Given:
// {
//   a: 2,
//   b: {b: 2, bb: {b: 3, bb: {b: 2}}},
//   c: {c: {c: 2}, cc: 'ball', ccc: 5},
//   d: 1,
//   e: {e: {e: 2}, ee: 'car'}
// }
nestedEvenSum(obj) → 10