Skip to content

rishiverma12031/statistics-calculator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

15 Commits
ย 
ย 
ย 
ย 

Repository files navigation

Statistics Calculator ๐Ÿ“Š

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.

Node.js JavaScript Math CLI

๐Ÿ“– About The Project

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.

Statistical Measures Calculated

  • 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)

โœจ Features

  • ๐Ÿ“ˆ 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

๐Ÿ“ธ Demo

$ 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.

Example with Mode

Numbers: 1, 2, 2, 3, 3, 3, 4, 5

Mean: 2.875
Median: 3
Mode: 3
Range: 4
Variance: 1.609375
Standard Deviation: 1.2686370808172522

Example with Multiple Modes

Numbers: 1, 1, 2, 3, 3, 4

Mean: 2.3333333333333335
Median: 2.5
Mode: 1, 3
Range: 3
Variance: 1.2222222222222223
Standard Deviation: 1.1055415967851332

๐Ÿ› ๏ธ Built With

  • 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)

๐Ÿ“‹ Prerequisites

  • Node.js (v12.0.0 or higher)
  • npm (Node Package Manager)

Check your installation:

node --version
npm --version

๐Ÿš€ Installation & Setup

1. Clone the Repository

git clone https://github.com/rishiverma12031/StatisticsCalculator.git
cd StatisticsCalculator

2. Install Dependencies

npm install prompt-sync chalk

3. Run the Calculator

node app.mjs

๐Ÿ’ป Usage

  1. Run the application: node app.mjs
  2. Enter numbers separated by commas when prompted
    • Example: 2, 4, 6, 8, 10
    • Spaces are automatically trimmed
  3. View all statistics displayed with explanations

Input Format

  • Comma-separated numbers: 1, 2, 3, 4, 5
  • Decimals supported: 1.5, 2.7, 3.9
  • Spaces optional: 1,2,3 or 1, 2, 3 both work
  • Negative numbers: -5, -2, 0, 3, 7

๐Ÿ“ Project Structure

StatisticsCalculator/
โ”‚
โ”œโ”€โ”€ app.mjs              # Main application file
โ”œโ”€โ”€ package.json         # Project dependencies (create this)
โ””โ”€โ”€ README.md           # Documentation

๐Ÿ”ง Code Structure & Algorithms

Main Functions

calculateMean(numbers)

const mean = numbers.reduce((sum, currentNumber) => sum += currentNumber, 0) / numbers.length;
  • Uses reduce() to sum all numbers
  • Divides by count for average

calculateMedian(numbers)

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

calculateMode(numbers)

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 null if all frequencies are equal

calculateRange(numbers)

const range = Math.max(...numbers) - Math.min(...numbers);
  • Uses spread operator with Math.max/min
  • Calculates difference

calculateVariance(numbers)

const squaredDeviations = numbers.map(deviation => (deviation - mean) ** 2);
const variance = calculateMean(squaredDeviations);
  • Calculates squared deviations from mean
  • Takes mean of squared deviations

calculateStandardDeviation(numbers)

const standardDeviation = Math.sqrt(variance);
  • Square root of variance

๐ŸŽ“ Learning Outcomes

This project demonstrates mastery of:

JavaScript Concepts

  • โœ… 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

Mathematical Concepts

  • Descriptive statistics formulas
  • Measures of central tendency (mean, median, mode)
  • Measures of dispersion (range, variance, standard deviation)
  • Data analysis fundamentals

Programming Practices

  • Clean code organization
  • Function reusability (variance uses mean)
  • Input parsing and data transformation
  • Educational output with explanations

๐Ÿงฎ Statistical Formulas Explained

Mean (Average)

Mean = (Sum of all numbers) / (Count of numbers)

Median (Middle Value)

If count is odd: Middle element of sorted array
If count is even: Average of two middle elements

Mode (Most Frequent)

The value(s) that appear most frequently
If all values appear equally: No mode (null)

Range (Spread)

Range = Maximum value - Minimum value

Variance (Dispersion)

Variance = Mean of squared deviations from the mean
= Average of [(xโ‚ - mean)ยฒ, (xโ‚‚ - mean)ยฒ, ..., (xโ‚™ - mean)ยฒ]

Standard Deviation

Standard Deviation = โˆšVariance

๐Ÿงช Test Cases

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

Edge Cases

  • 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")

๐ŸŽจ Customization Ideas

Easy Modifications

  • Add formatting to limit decimal places
  • Color-code statistics by type
  • Add data visualization with ASCII charts
  • Include count of numbers in output

Medium Challenges

  • Add quartiles (Q1, Q2, Q3)
  • Calculate percentiles
  • Add skewness and kurtosis
  • Support reading from CSV files
  • Add histogram display

Advanced Features

  • 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

๐Ÿ› Troubleshooting

Common Issues

Error: "Cannot find module 'prompt-sync'"

npm install prompt-sync

Error: "Cannot find module 'chalk'"

npm install chalk

NaN 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

๐ŸŽจ Color Scheme

The application uses Chalk for colored output:

  • ๐ŸŸฃ Magenta - Input prompt
  • ๐ŸŸข Green - User input field
  • ๐Ÿ”ท Cyan (Bold) - Statistical results
  • โšช White - Explanatory text

๐ŸŒŸ Future Enhancements

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

๐Ÿค Contributing

Contributions are welcome! Here's how:

  1. Fork the project
  2. Create your feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

Ideas for contributions:

  • Add more statistical measures
  • Improve input validation
  • Add unit tests
  • Create data visualization
  • Add export functionality

๐Ÿ“ License

This project is open source and available under the MIT License.

๐Ÿ‘ค Author

Rishi Verma

๐Ÿ™ Acknowledgments

  • 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

๐Ÿ“š Resources & References

๐Ÿ’ก Educational Note

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

About

CLI statistics calculator that computes mean, median, mode, range, variance, and standard deviation from a list of numbers. Built with Node.js using functional programming and array methods. ๐Ÿ“Š

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors