Skip to content
This repository has been archived by the owner on Sep 2, 2022. It is now read-only.

Commit

Permalink
Implement number average util
Browse files Browse the repository at this point in the history
  • Loading branch information
Ray Foo committed Aug 26, 2022
1 parent 04a06f6 commit a7d4f5f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/silver-cheetahs-jam.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@busyxiang/utilities-js': patch
---

Add number average util
15 changes: 15 additions & 0 deletions src/number.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { average } from './number';

describe('average', () => {
test('it should return average value', () => {
const aver = average(1, 2, 3);

expect(aver).toBe(2);
});

test('it should return average value(spread array)', () => {
const aver = average(...[1, 2, 3]);

expect(aver).toBe(2);
});
});
3 changes: 3 additions & 0 deletions src/number.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const average = (...nums: number[]) => {
return nums.reduce((acc, value) => acc + value, 0) / nums.length;
};

0 comments on commit a7d4f5f

Please sign in to comment.