A command-line statistics calculator that computes essential statistical measures from a list of numbers. Built with Node.js using functional programming principles and array methods to calculate mean, median, mode, range, variance, and standard deviation.
This CLI application takes a comma-separated list of numbers and calculates six fundamental statistical measures, displaying each result with a clear explanation of what it means. Perfect for students learning statistics, data analysts needing quick calculations, or developers practicing functional programming.
- Mean - The average of all numbers
- Median - The middle value when numbers are sorted
- Mode - The most frequently occurring number(s)
- Range - The difference between largest and smallest values
- Variance - How spread out the numbers are from the mean
- Standard Deviation - The square root of variance (measure of dispersion)
- ๐ Six Statistical Measures - Comprehensive descriptive statistics
- ๐จ Colorful Output - Using Chalk for enhanced readability
- ๐ Educational Explanations - Each statistic comes with a clear definition
- ๐ข Handles Multiple Modes - Displays all modes when multiple exist
- โ No Mode Detection - Returns null when all values appear equally
- ๐ฏ Functional Programming - Uses map, reduce, filter, forEach
- ๐ฏ Accurate Calculations - Implements correct statistical formulas
- ๐ฅ๏ธ Clean CLI Interface - Simple input, formatted output
$ node app.mjs
Enter a list of comma-seperated numbers
Numbers: 2, 4, 6, 8, 10
Mean: 6
The mean of a list of numbers is the average, calculated by taking the sum
of all numbers and dividing that by the count of numbers.
Median: 6
The median of a list of numbers is the number that appears in the middle
of the list, when sorted from least to greatest.
Mode: null
The mode of a list of numbers is the number that appears most often in the list.
Range: 8
The range of a list of numbers is the difference between the largest and
smallest numbers in the list.
Variance: 8
The variance of a list of numbers measures how far the values are from
the mean, on average.
Standard Deviation: 2.8284271247461903
The standard deviation of a list of numbers is the square root of the variance.Numbers: 1, 2, 2, 3, 3, 3, 4, 5
Mean: 2.875
Median: 3
Mode: 3
Range: 4
Variance: 1.609375
Standard Deviation: 1.2686370808172522Numbers: 1, 1, 2, 3, 3, 4
Mean: 2.3333333333333335
Median: 2.5
Mode: 1, 3
Range: 3
Variance: 1.2222222222222223
Standard Deviation: 1.1055415967851332- Node.js - JavaScript runtime environment
- ES6 Modules (.mjs) - Modern JavaScript syntax
- prompt-sync - Synchronous user input
- chalk - Terminal string styling and colors
- Functional Programming - Array methods (map, reduce, filter, forEach)
- Node.js (v12.0.0 or higher)
- npm (Node Package Manager)
Check your installation:
node --version
npm --versiongit clone https://github.com/rishiverma12031/StatisticsCalculator.git
cd StatisticsCalculatornpm install prompt-sync chalknode app.mjs- Run the application:
node app.mjs - Enter numbers separated by commas when prompted
- Example:
2, 4, 6, 8, 10 - Spaces are automatically trimmed
- Example:
- View all statistics displayed with explanations
- Comma-separated numbers:
1, 2, 3, 4, 5 - Decimals supported:
1.5, 2.7, 3.9 - Spaces optional:
1,2,3or1, 2, 3both work - Negative numbers:
-5, -2, 0, 3, 7
StatisticsCalculator/
โ
โโโ app.mjs # Main application file
โโโ package.json # Project dependencies (create this)
โโโ README.md # Documentation
const mean = numbers.reduce((sum, currentNumber) => sum += currentNumber, 0) / numbers.length;- Uses
reduce()to sum all numbers - Divides by count for average
const sortedNumbers = numbers.slice().sort((a, b) => a - b);- Sorts numbers in ascending order
- For even count: Average of two middle values
- For odd count: Middle value
const numbersWithCount = {};
numbers.forEach(number => numbersWithCount[number] = (numbersWithCount[number] || 0) + 1);- Creates frequency map using object
- Finds maximum frequency
- Returns all numbers with max frequency
- Returns
nullif all frequencies are equal
const range = Math.max(...numbers) - Math.min(...numbers);- Uses spread operator with Math.max/min
- Calculates difference
const squaredDeviations = numbers.map(deviation => (deviation - mean) ** 2);
const variance = calculateMean(squaredDeviations);- Calculates squared deviations from mean
- Takes mean of squared deviations
const standardDeviation = Math.sqrt(variance);- Square root of variance
This project demonstrates mastery of:
- โ Array Methods - map, reduce, filter, forEach, slice, sort
- โ Functional Programming - Pure functions, no side effects
- โ Object Manipulation - Creating frequency maps
- โ Spread Operator - Used with Math.max/min
- โ Arrow Functions - Concise function syntax
- โ Template Literals - String interpolation
- โ Ternary Operators - Conditional expressions
- โ Set Data Structure - Checking unique values
- Descriptive statistics formulas
- Measures of central tendency (mean, median, mode)
- Measures of dispersion (range, variance, standard deviation)
- Data analysis fundamentals
- Clean code organization
- Function reusability (variance uses mean)
- Input parsing and data transformation
- Educational output with explanations
Mean = (Sum of all numbers) / (Count of numbers)
If count is odd: Middle element of sorted array
If count is even: Average of two middle elements
The value(s) that appear most frequently
If all values appear equally: No mode (null)
Range = Maximum value - Minimum value
Variance = Mean of squared deviations from the mean
= Average of [(xโ - mean)ยฒ, (xโ - mean)ยฒ, ..., (xโ - mean)ยฒ]
Standard Deviation = โVariance
Try these datasets to test the calculator:
| Dataset | Mean | Median | Mode | Range | Variance | Std Dev |
|---|---|---|---|---|---|---|
2, 4, 6, 8, 10 |
6 | 6 | null | 8 | 8 | 2.83 |
1, 1, 2, 3, 3, 3 |
2.17 | 2.5 | 3 | 2 | 0.81 | 0.90 |
5, 5, 5, 5, 5 |
5 | 5 | null | 0 | 0 | 0 |
1, 2, 3, 4, 5, 6, 7, 8, 9, 10 |
5.5 | 5.5 | null | 9 | 8.25 | 2.87 |
10, 20, 30, 40, 50 |
30 | 30 | null | 40 | 200 | 14.14 |
- All same numbers: Mean = Median = number, Mode = null, Range = 0, Variance = 0
- Two numbers: Median = average of both
- Bimodal dataset: Mode shows multiple values (e.g., "1, 3")
- Add formatting to limit decimal places
- Color-code statistics by type
- Add data visualization with ASCII charts
- Include count of numbers in output
- Add quartiles (Q1, Q2, Q3)
- Calculate percentiles
- Add skewness and kurtosis
- Support reading from CSV files
- Add histogram display
- Create data visualization charts
- Add correlation analysis for two datasets
- Support multiple datasets comparison
- Build a web interface
- Add statistical significance tests
- Export results to file
Error: "Cannot find module 'prompt-sync'"
npm install prompt-syncError: "Cannot find module 'chalk'"
npm install chalkNaN or undefined results
- Ensure input contains only numbers separated by commas
- Check for proper number formatting
Incorrect median calculation
- The code properly handles both even and odd length arrays
- Uses
slice()to avoid mutating original array
The application uses Chalk for colored output:
- ๐ฃ Magenta - Input prompt
- ๐ข Green - User input field
- ๐ท Cyan (Bold) - Statistical results
- โช White - Explanatory text
Planned features:
- Add more statistics (quartiles, IQR, skewness, kurtosis)
- Support file input (CSV, JSON)
- Add data visualization (histograms, box plots)
- Export results to file
- Batch processing of multiple datasets
- Web interface version
- Add confidence intervals
- Support weighted statistics
- Include hypothesis testing
- Add correlation and regression analysis
Contributions are welcome! Here's how:
- Fork the project
- Create your feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
Ideas for contributions:
- Add more statistical measures
- Improve input validation
- Add unit tests
- Create data visualization
- Add export functionality
This project is open source and available under the MIT License.
Rishi Verma
- GitHub: @rishiverma12031
- Project Link: https://github.com/rishiverma12031/StatisticsCalculator
- Built to practice functional programming and array methods
- Uses prompt-sync for user input
- Uses chalk for terminal colors
- Implements standard statistical formulas and methods
- Statistics Fundamentals - Khan Academy
- MDN - Array Methods
- Descriptive Statistics
- Standard Deviation Explained
This calculator implements statistical formulas from scratch using JavaScript array methods and functional programming. While libraries like Math.js or simple-statistics exist, building from scratch helps understand:
- How statistical calculations work
- Functional programming patterns
- Array method applications
- Mathematical algorithm implementation
Ready to calculate? Run node app.mjs and enter your data! ๐
Built with โค๏ธ while learning functional programming and statistics in JavaScript