Finish quiz 2

This commit is contained in:
Isaac 2022-10-26 10:18:05 -03:00
parent 9a62c4918f
commit e18f58c0d0
2 changed files with 18 additions and 1 deletions

View File

@ -1,6 +1,13 @@
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)

View File

@ -16,5 +16,15 @@
function() {
let obj = [1, 2, [-6, 2, -2], 3]
expect(sum.objectSum(obj)).toBe(0);
});
});
it("Lists",
function() {
let obj = {a: 1, b: 2, c: 3}
expect(sum.objectSum(obj)).toBe(6);
});
it("Recursive lists",
function() {
let obj = [1, 2, {thing: [-6, -12], other: 6}]
expect(sum.objectSum(obj)).toBe(-9);
});
});