Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

15 changes: 15 additions & 0 deletions src/utils/cal.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
};
39 changes: 38 additions & 1 deletion src/utils/cal.test.js
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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);
}
);
});