Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 19ecb18

Browse files
fix: improve mean function with input validation and extended tests (#1785)
1 parent 3f52add commit 19ecb18

File tree

2 files changed

+63
-6
lines changed

2 files changed

+63
-6
lines changed

‎Maths/AverageMean.js

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,30 @@
11
/**
22
* @function mean
33
* @description This script will find the mean value of a array of numbers.
4-
* @param {Integer[]} nums - Array of integer
5-
* @return {Integer} - mean of nums.
4+
* @param {number[]} numbers - Array of integer
5+
* @return {number} - mean of numbers.
6+
* @throws {TypeError} If the input is not an array or contains non-number elements.
7+
* @throws {Error} If the input array is empty.
68
* @see [Mean](https://en.wikipedia.org/wiki/Mean)
79
* @example mean([1, 2, 4, 5]) = 3
810
* @example mean([10, 40, 100, 20]) = 42.5
911
*/
10-
11-
const mean = (nums) => {
12-
if (!Array.isArray(nums)) {
12+
const mean = (numbers) => {
13+
if (!Array.isArray(numbers)) {
1314
throw new TypeError('Invalid Input')
15+
} else if (numbers.length === 0) {
16+
throw new Error('Array is empty')
1417
}
1518

16-
return nums.reduce((sum, cur) => sum + cur, 0) / nums.length
19+
let total = 0
20+
numbers.forEach((num) => {
21+
if (typeof num !== 'number') {
22+
throw new TypeError('Invalid Input')
23+
}
24+
total += num
25+
})
26+
27+
return total / numbers.length
1728
}
1829

1930
export { mean }

‎Maths/test/AverageMean.test.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,50 @@ describe('Tests for average mean', () => {
1818
const meanFunction = mean([10, 40, 100, 20])
1919
expect(meanFunction).toBe(42.5)
2020
})
21+
22+
it('should return the number itself for a single-element array', () => {
23+
const result = mean([5])
24+
expect(result).toBe(5)
25+
})
26+
27+
it('should throw error for empty array', () => {
28+
expect(() => mean([])).toThrow()
29+
})
30+
31+
it('should throw error for an array containing null', () => {
32+
expect(() => mean([1, 2, null])).toThrow()
33+
})
34+
35+
it('should throw error for an array containing booleans', () => {
36+
expect(() => mean([1, 2, true])).toThrow()
37+
})
38+
39+
it('should throw error for an array containing strings', () => {
40+
expect(() => mean([1, 2, 'asd'])).toThrow()
41+
})
42+
43+
it('should return the mean of an array with negative numbers', () => {
44+
const result = mean([-1, -2, -3, -4])
45+
expect(result).toBe(-2.5)
46+
})
47+
48+
it('should return the mean of an array with floating-point numbers', () => {
49+
const result = mean([1.5, 2.5, 3.5])
50+
expect(result).toBe(2.5)
51+
})
52+
53+
it('should return 0 for an array with zeros', () => {
54+
const result = mean([0, 0, 0])
55+
expect(result).toBe(0)
56+
})
57+
58+
it('should handle very large numbers correctly', () => {
59+
const result = mean([1000000000, 2000000000, 3000000000])
60+
expect(result).toBe(2000000000)
61+
})
62+
63+
it('should return correct mean for an array with integers and floating-point numbers', () => {
64+
const result = mean([1, 2.5, 3])
65+
expect(result).toBeCloseTo(2.1667, 4)
66+
})
2167
})

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /