diff --git a/README.md b/README.md index 58beeac..45240a7 100644 --- a/README.md +++ b/README.md @@ -68,3 +68,4 @@ This section has moved here: [https://facebook.github.io/create-react-app/docs/d ### `npm run build` fails to minify This section has moved here: [https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify](https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify) + diff --git a/src/utils/cal.js b/src/utils/cal.js index cb801eb..8238d03 100644 --- a/src/utils/cal.js +++ b/src/utils/cal.js @@ -5,3 +5,18 @@ * @returns */ export const add = (a, b) => a + b; + +export const sum = (nums) => { + if (nums === undefined) return undefined; + if (nums.length === 0) return undefined; + + return nums.reduce((acc, row) => acc + row, 0); +}; + +export const divide = (a, b) => { + + if (b === 0) { + return undefined; + } + return { quotient: Math.floor(a / b), remainder: a % b }; +}; diff --git a/src/utils/cal.test.js b/src/utils/cal.test.js index 6584f06..5a7732d 100644 --- a/src/utils/cal.test.js +++ b/src/utils/cal.test.js @@ -1,4 +1,4 @@ -import { add } from './cal'; +import { add, sum, divide } from './cal'; test('adds 1 + 2 to equal 3', () => { const result = add(1, 2); @@ -19,3 +19,40 @@ describe('add test', () => { } ); }); + + +describe('sum test', () => { + // add test cases here + const cases = [ + [undefined, undefined], + [[], undefined], + [[1], 1], + [[1, 2], 3], + ]; + test.each(cases)( + 'test case sum [%p] = %p', + (nums, expected) => { + const result = sum(nums); + expect(result).toBe(expected); + } + ); +}); + +describe('divide test', () => { + // add test cases here + const cases = [ + [1, 0, undefined], + [1, 1, { quotient: 1, remainder: 0 }], + [2, 1, { quotient: 2, remainder: 0 }], + [3, 1, { quotient: 3, remainder: 0 }], + [3, 2, { quotient: 1, remainder: 1 }], + [3, 3, { quotient: 1, remainder: 0 }], + ]; + test.each(cases)( + 'test case divide %p / %p %p', + (a, b, expected) => { + const result = divide(a, b); + expect(result).toEqual(expected); + } + ); +});