From 34731c04f4df66c2b4b41d44820c04bd5a49bc54 Mon Sep 17 00:00:00 2001 From: Justin O'Reilly Date: Fri, 21 May 2021 18:49:49 -0400 Subject: [PATCH] Added Jest --- sumAll/sumAll.js | 14 ++++++++++++-- sumAll/sumAll.spec.js | 22 +++++++++++----------- 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/sumAll/sumAll.js b/sumAll/sumAll.js index aab00af..81ea23b 100644 --- a/sumAll/sumAll.js +++ b/sumAll/sumAll.js @@ -1,5 +1,15 @@ -const sumAll = function() { - +const sumAll = function (numStart, numEnd) { + // final sum variable + let sum = 0; + if (numStart < 0 || numEnd < 0 || typeof numStart != "number" || typeof numEnd != "number") { + return "ERROR"; + } else if (numStart > numEnd) { + [numStart, numEnd] = [numEnd, numStart]; + } + for (let i = numStart; i <= numEnd; i++) { + sum += i; + } + return sum; }; module.exports = sumAll; diff --git a/sumAll/sumAll.spec.js b/sumAll/sumAll.spec.js index 1a9fb7c..91d5a27 100644 --- a/sumAll/sumAll.spec.js +++ b/sumAll/sumAll.spec.js @@ -1,22 +1,22 @@ -const sumAll = require('./sumAll') +const sumAll = require("./sumAll"); -describe('sumAll', () => { - test('sums numbers within the range', () => { +describe("sumAll", () => { + test("sums numbers within the range", () => { expect(sumAll(1, 4)).toEqual(10); }); - test.skip('works with large numbers', () => { + test("works with large numbers", () => { expect(sumAll(1, 4000)).toEqual(8002000); }); - test.skip('works with larger number first', () => { + test("works with larger number first", () => { expect(sumAll(123, 1)).toEqual(7626); }); - test.skip('returns ERROR with negative numbers', () => { - expect(sumAll(-10, 4)).toEqual('ERROR'); + test("returns ERROR with negative numbers", () => { + expect(sumAll(-10, 4)).toEqual("ERROR"); }); - test.skip('returns ERROR with non-number parameters', () => { - expect(sumAll(10, "90")).toEqual('ERROR'); + test("returns ERROR with non-number parameters", () => { + expect(sumAll(10, "90")).toEqual("ERROR"); }); - test.skip('returns ERROR with non-number parameters', () => { - expect(sumAll(10, [90, 1])).toEqual('ERROR'); + test("returns ERROR with non-number parameters", () => { + expect(sumAll(10, [90, 1])).toEqual("ERROR"); }); });