Compare commits
2 Commits
8f668a032f
...
e18f58c0d0
Author | SHA1 | Date | |
---|---|---|---|
|
e18f58c0d0 | ||
|
9a62c4918f |
21
tests/Q2/objectSum.js
Normal file
21
tests/Q2/objectSum.js
Normal file
@ -0,0 +1,21 @@
|
||||
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
|
30
tests/Q2/spec/objectSum.spec.js
Normal file
30
tests/Q2/spec/objectSum.spec.js
Normal file
@ -0,0 +1,30 @@
|
||||
let sum=require ("../objectSum.js");
|
||||
|
||||
describe("objectSum",
|
||||
function() {
|
||||
it("List of numbers",
|
||||
function() {
|
||||
let obj = [1, 2, 3]
|
||||
expect(sum.objectSum(obj)).toBe(6);
|
||||
});
|
||||
it("Skip non numbers",
|
||||
function() {
|
||||
let obj = [1, 2, "jellyfish", 3]
|
||||
expect(sum.objectSum(obj)).toBe(6);
|
||||
});
|
||||
it("Recursive arrays",
|
||||
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);
|
||||
});
|
||||
});
|
13
tests/Q2/spec/support/jasmine.json
Normal file
13
tests/Q2/spec/support/jasmine.json
Normal file
@ -0,0 +1,13 @@
|
||||
{
|
||||
"spec_dir": "spec",
|
||||
"spec_files": [
|
||||
"**/*[sS]pec.?(m)js"
|
||||
],
|
||||
"helpers": [
|
||||
"helpers/**/*.?(m)js"
|
||||
],
|
||||
"env": {
|
||||
"stopSpecOnExpectationFailure": false,
|
||||
"random": true
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user