function sumNestedArray(arr) {
  // Base case: when the array is empty, return 0
  if (arr.length === 0) {
    return 0;
  }

  let sum = 0;

  for (let element of arr) {
    if (Array.isArray(element)) {
      // Recursive case: if the element is an array, sum its elements
      sum += sumNestedArray(element);
    } else if (typeof element === 'number') {
      sum += element;
    }
  }

  return sum;
}

// Test cases
console.log(sumNestedArray([1, [2, [3, 4], 5], 6])); // => 21
console.log(sumNestedArray([[[[1]]], 2, [3, [4, [5]]]])); // => 15
console.log(sumNestedArray([])); // => 0
console.log(sumNestedArray([10, [20, [30, [40]]]])); // => 100
console.log(sumNestedArray([1, 'a', [2, 'b', [3, 4]], 5])); // => 15 (ignores non-numbers)
