Content-Length: 3456 | pFad | http://github.com/TheAlgorithms/JavaScript/pull/1785.patch
thub.com
From 2b05c6b3c23e3160009a790d3388bbf3c6adc19d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fatih=20BARA=C3=87KILI=C3=87?=
Date: Sun, 20 Jul 2025 10:10:51 +0300
Subject: [PATCH] fix: improve mean function with input validation and extended
tests
---
Maths/AverageMean.js | 23 ++++++++++++-----
Maths/test/AverageMean.test.js | 46 ++++++++++++++++++++++++++++++++++
2 files changed, 63 insertions(+), 6 deletions(-)
diff --git a/Maths/AverageMean.js b/Maths/AverageMean.js
index 8ae3b55992..3df7b2769b 100644
--- a/Maths/AverageMean.js
+++ b/Maths/AverageMean.js
@@ -1,19 +1,30 @@
/**
* @function mean
* @description This script will find the mean value of a array of numbers.
- * @param {Integer[]} nums - Array of integer
- * @return {Integer} - mean of nums.
+ * @param {number[]} numbers - Array of integer
+ * @return {number} - mean of numbers.
+ * @throws {TypeError} If the input is not an array or contains non-number elements.
+ * @throws {Error} If the input array is empty.
* @see [Mean](https://en.wikipedia.org/wiki/Mean)
* @example mean([1, 2, 4, 5]) = 3
* @example mean([10, 40, 100, 20]) = 42.5
*/
-
-const mean = (nums) => {
- if (!Array.isArray(nums)) {
+const mean = (numbers) => {
+ if (!Array.isArray(numbers)) {
throw new TypeError('Invalid Input')
+ } else if (numbers.length === 0) {
+ throw new Error('Array is empty')
}
- return nums.reduce((sum, cur) => sum + cur, 0) / nums.length
+ let total = 0
+ numbers.forEach((num) => {
+ if (typeof num !== 'number') {
+ throw new TypeError('Invalid Input')
+ }
+ total += num
+ })
+
+ return total / numbers.length
}
export { mean }
diff --git a/Maths/test/AverageMean.test.js b/Maths/test/AverageMean.test.js
index 7d0bd3a6ac..ce9109b916 100644
--- a/Maths/test/AverageMean.test.js
+++ b/Maths/test/AverageMean.test.js
@@ -18,4 +18,50 @@ describe('Tests for average mean', () => {
const meanFunction = mean([10, 40, 100, 20])
expect(meanFunction).toBe(42.5)
})
+
+ it('should return the number itself for a single-element array', () => {
+ const result = mean([5])
+ expect(result).toBe(5)
+ })
+
+ it('should throw error for empty array', () => {
+ expect(() => mean([])).toThrow()
+ })
+
+ it('should throw error for an array containing null', () => {
+ expect(() => mean([1, 2, null])).toThrow()
+ })
+
+ it('should throw error for an array containing booleans', () => {
+ expect(() => mean([1, 2, true])).toThrow()
+ })
+
+ it('should throw error for an array containing strings', () => {
+ expect(() => mean([1, 2, 'asd'])).toThrow()
+ })
+
+ it('should return the mean of an array with negative numbers', () => {
+ const result = mean([-1, -2, -3, -4])
+ expect(result).toBe(-2.5)
+ })
+
+ it('should return the mean of an array with floating-point numbers', () => {
+ const result = mean([1.5, 2.5, 3.5])
+ expect(result).toBe(2.5)
+ })
+
+ it('should return 0 for an array with zeros', () => {
+ const result = mean([0, 0, 0])
+ expect(result).toBe(0)
+ })
+
+ it('should handle very large numbers correctly', () => {
+ const result = mean([1000000000, 2000000000, 3000000000])
+ expect(result).toBe(2000000000)
+ })
+
+ it('should return correct mean for an array with integers and floating-point numbers', () => {
+ const result = mean([1, 2.5, 3])
+ expect(result).toBeCloseTo(2.1667, 4)
+ })
})
--- a PPN by Garber Painting Akron. With Image Size Reduction included!Fetched URL: http://github.com/TheAlgorithms/JavaScript/pull/1785.patch
Alternative Proxies:
Alternative Proxy
pFad Proxy
pFad v3 Proxy
pFad v4 Proxy