Finish quiz 2
This commit is contained in:
parent
9a62c4918f
commit
e18f58c0d0
@ -1,6 +1,13 @@
|
|||||||
function objectSum(obj) {
|
function objectSum(obj) {
|
||||||
sum = 0
|
sum = 0
|
||||||
for (let number of obj){
|
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)
|
//Case for when the number is actually not a number (eg, recursive list)
|
||||||
if (typeof(number) === "object")
|
if (typeof(number) === "object")
|
||||||
sum = sum + objectSum(number)
|
sum = sum + objectSum(number)
|
||||||
|
@ -17,4 +17,14 @@
|
|||||||
let obj = [1, 2, [-6, 2, -2], 3]
|
let obj = [1, 2, [-6, 2, -2], 3]
|
||||||
expect(sum.objectSum(obj)).toBe(0);
|
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);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user