CS2613/tests/Q2/objectSum.js
2022-10-26 10:18:05 -03:00

22 lines
537 B
JavaScript

function objectSum(obj) {
sum = 0
for (let number of obj){
// Attempted to support objects
//if (typeof(number[1]) === "number")
// sum = sum + number[1]
//if (typeof(number[1]) === "object")
// sum = sum + objectSum(number[1])
//Case for when the number is actually not a number (eg, recursive list)
if (typeof(number) === "object")
sum = sum + objectSum(number)
//Case for when the number is a number
if(typeof(number) === "number")
sum = sum + number
}
return sum
}
exports.objectSum = objectSum