Content-Length: 348059 | pFad | http://github.com/TheAlgorithms/JavaScript/pull/1785/files

26 fix: improve mean function with input validation and extended tests by FatihBARACKILIC · Pull Request #1785 · TheAlgorithms/JavaScript · GitHub
Skip to content

fix: improve mean function with input validation and extended tests #1785

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions Maths/AverageMean.js
Original file line number Diff line number Diff line change
@@ -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 }
46 changes: 46 additions & 0 deletions Maths/test/AverageMean.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})
Loading








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/TheAlgorithms/JavaScript/pull/1785/files

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy