CS2613/tests/Q2/objectSum.js

22 lines
537 B
JavaScript
Raw Normal View History

2022-10-26 10:10:09 -03:00
function objectSum(obj) {
sum = 0
for (let number of obj){
2022-10-26 10:18:05 -03:00
// Attempted to support objects
//if (typeof(number[1]) === "number")
// sum = sum + number[1]
//if (typeof(number[1]) === "object")
// sum = sum + objectSum(number[1])
2022-10-26 10:10:09 -03:00
//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