JavaScript/07-nestedEvenSum.js
// Return the sum of all even numbers in an object which may contain nested objects
Return the sum of all even numbers in an object which may contain nested objects
Return the sum of all even numbers in an object which may contain nested objects
function nestedEvenSum(obj) {
  let sum = 0;
  function helper(helperObj) {
    for (let el of Object.values(helperObj)) {
      if (typeof el === "number" && el % 2 === 0) {
        sum += el;
      } else if (typeof el === "object") {
        helper(Object.values(el));
      }
    }
  }
  helper(obj);
  return sum;
}
function nestedEvenSum ( obj ) { let sum = 0 ; function helper ( helperObj ) { for ( let el of Object . values ( helperObj ) ) { if ( typeof el === "number" && el % 2 === 0 ) { sum += el ; } else if ( typeof el === "object" ) { helper ( Object . values ( el ) ) ; } } } helper ( obj ) ; return sum ; }
Return the sum of all even numbers in an object which may contain nested objects
Return the sum of all even numbers in an object which may contain nested objects
// if (!el) return; // If el is undefined, return null(base case) // If element is even number, add up to sum // If element is object, recur