Skip to content

Commit

Permalink
add(math): average functionality (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
shivam-sharma7 authored Dec 7, 2024
1 parent 4cff757 commit eb47a67
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
10 changes: 9 additions & 1 deletion src/maths/basic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,12 @@ export const divide = (...args: number[]): number => {
return rest.reduce((acc, num) => (num !== 0 ? acc / num : acc), first);
};

// TODO : Add more math operations
/**
* Return the average of numbers
*/

export const average = (...args: number[]): number => {
if (args.length === 0) return 0;
const total = args.reduce((acc, num) => acc + num, 0);
return total / args.length;
};
8 changes: 7 additions & 1 deletion tests/math.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, it, expect } from 'vitest';
import { sum, subtract, multiplication, divide } from '../src/maths/basic.js';
import { sum, subtract, multiplication, divide, average } from '../src/maths/basic.js';

describe('Math module helper', () => {
it('should pass sum', () => {
Expand All @@ -25,4 +25,10 @@ describe('Math module helper', () => {
let b = 20;
expect(divide(a, b));
});
it('should pass average', () => {
let a = 10;
let b = 20;
let c = 30;
expect(average(a, b, c)).toBe(20);
});
});

0 comments on commit eb47a67

Please sign in to comment.