Repository navigation
#221 Basic Statistics Mean
The mean of a group of numbers is calculated by summing all numbers, and dividing this sum by the total count of numbers in the group.
Given a sorted array of numbers, return the mean(rounded to one decimal place).
Current source and tests | Catalog | Testing and tooling
From the cloned repository root, after installing dependencies:
node "Edabit/221 Basic Statistics Mean.js"Assertions are kept alongside the implementation and throw on failure. A successful run exits with code 0. Run every exercise with npm test or both lint and tests with npm run check.
This is the complete executable file, including its imports and inline test cases. Run it from the repository, not as an isolated file copied out of the wiki. Line endings and trailing whitespace are normalized; executable tokens are unchanged.
//The mean of a group of numbers is calculated by summing all numbers, and dividing this sum by the total count of numbers in the group.
//Given a sorted array of numbers, return the mean(rounded to one decimal place).
function mean(nums) {
return Number.parseFloat((nums.reduce((a, b) => a + b, 0) / nums.length).toFixed(1));
}
import { assertEquals } from '../testHelper.js';
assertEquals(mean([1, 1, 2, 2, 3, 5, 5, 5, 7]), 3.4)
assertEquals(mean([1, 2, 2, 3, 7, 9, 10]), 4.9)
assertEquals(mean([1, 2, 3, 4, 5, 6, 6, 10, 10]), 5.2)
assertEquals(mean([5, 5, 6, 7, 10, 10]), 7.2)
assertEquals(mean([1, 3, 3, 4, 5, 6, 7, 8]), 4.6)
assertEquals(mean([2, 4, 6, 6, 6, 8, 9]), 5.9)
assertEquals(mean([2, 4, 6, 6, 6, 7, 7, 8, 9]), 6.1)
assertEquals(mean([2, 3, 4, 7, 8, 8]), 5.3)
assertEquals(mean([1, 2, 2, 2, 3, 4, 4, 4, 6]), 3.1)
assertEquals(mean([2, 2, 8, 9, 9, 10, 10]), 7.1)
assertEquals(mean([1, 3, 3, 4, 5, 5, 7, 9]), 4.6)
assertEquals(mean([1, 1, 3, 4, 4, 4, 7, 9, 10]), 4.8)
assertEquals(mean([1, 2, 3, 6, 6, 9]), 4.5)
assertEquals(mean([3, 5, 7, 7, 9, 10, 10]), 7.3)
assertEquals(mean([1, 2, 4, 4, 6, 6]), 3.8)
assertEquals(mean([2, 4, 7, 7, 8, 8, 9, 9, 10]), 7.1)
assertEquals(mean([2, 4, 4, 4, 5, 5, 8, 9, 10]), 5.7)
assertEquals(mean([1, 6, 6, 8, 10, 10]), 6.8)
assertEquals(mean([2, 6, 6, 9, 10, 10]), 7.2)
assertEquals(mean([1, 4, 4, 4, 7, 8, 9]), 5.3)