From eb47a676e588abace60bacd3aff42e79eecdf42a Mon Sep 17 00:00:00 2001 From: Shivam Sharma Date: Sat, 7 Dec 2024 21:01:00 +0530 Subject: [PATCH] add(math): average functionality (#50) --- src/maths/basic.ts | 10 +++++++++- tests/math.test.ts | 8 +++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/maths/basic.ts b/src/maths/basic.ts index 4698685..0f1f8e0 100644 --- a/src/maths/basic.ts +++ b/src/maths/basic.ts @@ -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; +}; diff --git a/tests/math.test.ts b/tests/math.test.ts index 04bf864..7a939cc 100644 --- a/tests/math.test.ts +++ b/tests/math.test.ts @@ -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', () => { @@ -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); + }); });