Recursive function not returning sum of object values

Why sum is undefined. What am I doing wrong?

var testData1 = [{
  "name": "Doctors",
  "category": "Doctors",
  "subCategory": [{
    "name": "Likes",
    "category": "Likes",
    "subCategory": [{
      "name": "Visit",
      "category": "Visit",
      "subCategory": [{
        "name": "Charges",
        "category": "Charges",
        "subCategory": null,
        "val": 30
      }]
    }]
  }, {
    "name": "Dislikes",
    "category": "Dislikes",
    "subCategory": [{
      "name": "Quality",
      "category": "Quality",
      "subCategory": [{
        "name": "Appointment",
        "category": "Appointment",
        "subCategory": null,
        "val": 50
      }, {
        "name": "Care",
        "category": "Care",
        "subCategory": null,
        "val": 70
      }, {
        "name": "Attentive ",
        "category": "Attentive ",
        "subCategory": null,
        "val": 90
      }]
    }]
  }, {
    "name": "Neutral",
    "category": "Neutral",
    "subCategory": [{
      "name": "Professional ",
      "category": "Professional ",
      "subCategory": [{
        "name": "Ease",
        "category": "Ease",
        "subCategory": null,
        "val": 50
      }, {
        "name": "Competent ",
        "category": "Competent ",
        "subCategory": null,
        "val": 40
      }, {
        "name": "Availability",
        "category": "Availability",
        "subCategory": null,
        "val": 80
      }]
    }]
  }],
  "index": 6
}, {
  "name": "Service",
  "category": "Service",
  "subCategory": [{
    "name": "Likes",
    "category": "Likes",
    "subCategory": [{
      "name": "Environment",
      "category": "Environment",
      "subCategory": [{
        "name": "Professionalism ",
        "category": "Professionalism ",
        "subCategory": null,
        "val": 50
      }, {
        "name": "Room",
        "category": "Room",
        "subCategory": null,
        "val": 30
      }, {
        "name": "Parking",
        "category": "Parking",
        "subCategory": null,
        "val": 20
      }]
    }, {
      "name": "Availability",
      "category": "Availability",
      "subCategory": [{
        "name": "Competent ",
        "category": "Competent ",
        "subCategory": null,
        "val": 30
      }]
    }]
  }, {
    "name": "Dislikes",
    "category": "Dislikes",
    "subCategory": [{
      "name": "Management",
      "category": "Management",
      "subCategory": [{
        "name": "Staff",
        "category": "Staff",
        "subCategory": null,
        "val": 50
      }, {
        "name": "Operations",
        "category": "Operations",
        "subCategory": null,
        "val": 70
      }]
    }, {
      "name": "Nurses",
      "category": "Nurses",
      "subCategory": [{
        "name": "Medicine",
        "category": "Medicine",
        "subCategory": null,
        "val": 30
      }]
    }]
  }, {
    "name": "Neutral",
    "category": "Neutral",
    "subCategory": [{
      "name": "Serving",
      "category": "Serving",
      "subCategory": [{
        "name": "Took long time",
        "category": "Took long time",
        "subCategory": null,
        "val": 50
      }, {
        "name": "Rude",
        "category": "Rude",
        "subCategory": null,
        "val": 40
      }, {
        "name": "Seated",
        "category": "Seated",
        "subCategory": null,
        "val": 80
      }]
    }]
  }],
  "index": 16
}];

function addSum(data) {
  data.forEach(function(d, index) {
    if (Array.isArray(d.subCategory)) {
      return (0 + addSum(d.subCategory));

    } else {

      document.write('<pre>' + JSON.stringify(d.val, 0, 4) + '</pre>');
      return d.val;
    }
  });
}
var sum = addSum(testData1);
alert(sum);

Click here view more answers http://ift.tt/1YSbrWb