From 9a62c4918f91e221e9ed95e28430246eedebbd1c Mon Sep 17 00:00:00 2001 From: Isaac Date: Wed, 26 Oct 2022 10:10:09 -0300 Subject: [PATCH] Add quiz 2 content --- tests/Q2/objectSum.js | 14 ++++++++++++++ tests/Q2/spec/objectSum.spec.js | 20 ++++++++++++++++++++ tests/Q2/spec/support/jasmine.json | 13 +++++++++++++ 3 files changed, 47 insertions(+) create mode 100644 tests/Q2/objectSum.js create mode 100644 tests/Q2/spec/objectSum.spec.js create mode 100644 tests/Q2/spec/support/jasmine.json diff --git a/tests/Q2/objectSum.js b/tests/Q2/objectSum.js new file mode 100644 index 0000000..4858fa8 --- /dev/null +++ b/tests/Q2/objectSum.js @@ -0,0 +1,14 @@ +function objectSum(obj) { + sum = 0 + for (let number of obj){ + //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 diff --git a/tests/Q2/spec/objectSum.spec.js b/tests/Q2/spec/objectSum.spec.js new file mode 100644 index 0000000..f195ade --- /dev/null +++ b/tests/Q2/spec/objectSum.spec.js @@ -0,0 +1,20 @@ + 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); + }); + }); diff --git a/tests/Q2/spec/support/jasmine.json b/tests/Q2/spec/support/jasmine.json new file mode 100644 index 0000000..6afe603 --- /dev/null +++ b/tests/Q2/spec/support/jasmine.json @@ -0,0 +1,13 @@ +{ + "spec_dir": "spec", + "spec_files": [ + "**/*[sS]pec.?(m)js" + ], + "helpers": [ + "helpers/**/*.?(m)js" + ], + "env": { + "stopSpecOnExpectationFailure": false, + "random": true + } +}