.calculator-container {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background-color: #f9f9f9;
border: 1px solid #ddd;
border-radius: 8px;
padding: 20px;
max-width: 600px;
margin: 20px auto;
box-shadow: 0 4px 8px rgba(0,0,0,0.05);
}
.calculator-container h2 {
color: #333;
text-align: center;
margin-bottom: 20px;
font-size: 24px;
}
.calculator-content .input-group {
margin-bottom: 15px;
}
.calculator-content label {
display: block;
margin-bottom: 8px;
color: #555;
font-weight: bold;
}
.calculator-content input[type="text"] {
width: calc(100% – 20px);
padding: 10px;
border: 1px solid #ccc;
border-radius: 4px;
font-size: 16px;
}
.calculator-content .calculate-button {
display: block;
width: 100%;
padding: 12px 20px;
background-color: #007bff;
color: white;
border: none;
border-radius: 4px;
font-size: 18px;
cursor: pointer;
transition: background-color 0.3s ease;
margin-top: 20px;
}
.calculator-content .calculate-button:hover {
background-color: #0056b3;
}
.calculator-content .result-group {
margin-top: 25px;
padding-top: 20px;
border-top: 1px solid #eee;
}
.calculator-content .result-group h3 {
color: #333;
margin-bottom: 15px;
font-size: 20px;
}
.calculator-content .result-group p {
margin-bottom: 10px;
color: #444;
font-size: 16px;
}
.calculator-content .result-group p strong {
color: #007bff;
}
.calculator-content .result-group span {
font-weight: normal;
color: #222;
}
function calculateStatistics() {
var numbersInput = document.getElementById("numbersInput").value;
var numbersArray = numbersInput.split(',').map(function(item) {
return parseFloat(item.trim());
}).filter(function(item) {
return !isNaN(item);
});
if (numbersArray.length === 0) {
document.getElementById("resultMean").innerText = "Please enter valid numbers.";
document.getElementById("resultMedian").innerText = "";
document.getElementById("resultMode").innerText = "";
document.getElementById("resultRange").innerText = "";
return;
}
// Sort the array for median and range
numbersArray.sort(function(a, b) { return a – b; });
var mean = calculateMean(numbersArray);
var median = calculateMedian(numbersArray);
var mode = calculateMode(numbersArray);
var range = calculateRange(numbersArray);
document.getElementById("resultMean").innerText = mean.toFixed(2);
document.getElementById("resultMedian").innerText = median.toFixed(2);
document.getElementById("resultMode").innerText = mode;
document.getElementById("resultRange").innerText = range.toFixed(2);
}
function calculateMean(arr) {
if (arr.length === 0) return 0;
var sum = arr.reduce(function(a, b) { return a + b; }, 0);
return sum / arr.length;
}
function calculateMedian(arr) {
if (arr.length === 0) return 0;
var mid = Math.floor(arr.length / 2);
if (arr.length % 2 === 0) {
return (arr[mid – 1] + arr[mid]) / 2;
} else {
return arr[mid];
}
}
function calculateMode(arr) {
if (arr.length === 0) return "N/A";
if (arr.length === 1) return arr[0].toFixed(2);
var frequencyMap = {};
arr.forEach(function(number) {
frequencyMap[number] = (frequencyMap[number] || 0) + 1;
});
var maxFrequency = 0;
for (var key in frequencyMap) {
if (frequencyMap[key] > maxFrequency) {
maxFrequency = frequencyMap[key];
}
}
var modes = [];
for (var key in frequencyMap) {
if (frequencyMap[key] === maxFrequency) {
modes.push(parseFloat(key));
}
}
if (maxFrequency === 1 && arr.length > 1) {
return "No distinct mode (all numbers appear once)";
} else {
return modes.map(function(m) { return m.toFixed(2); }).join(', ');
}
}
function calculateRange(arr) {
if (arr.length === 0) return 0;
return arr[arr.length – 1] – arr[0];
}
Understanding Mean, Median, Mode, and Range
In statistics, mean, median, mode, and range are fundamental concepts used to describe and summarize a set of numerical data. They provide different insights into the central tendency and spread of the data, helping us understand its characteristics at a glance.
What is the Mean?
The mean, often referred to as the average, is calculated by summing all the values in a dataset and then dividing by the total number of values. It's the most commonly used measure of central tendency and is sensitive to outliers (extremely high or low values).
Formula: Mean = (Sum of all values) / (Number of values)
Example: For the numbers 10, 20, 30, 40, 50:
- Sum = 10 + 20 + 30 + 40 + 50 = 150
- Number of values = 5
- Mean = 150 / 5 = 30
What is the Median?
The median is the middle value in a dataset when the values are arranged in ascending or descending order. It's a robust measure of central tendency, meaning it's less affected by outliers compared to the mean.
- If the number of values is odd: The median is the single middle value.
- If the number of values is even: The median is the average of the two middle values.
Example 1 (Odd number of values): For the numbers 10, 20, 30, 40, 50 (already sorted):
- The middle value is 30.
- Median = 30
Example 2 (Even number of values): For the numbers 10, 20, 30, 40, 50, 60 (sorted):
- The two middle values are 30 and 40.
- Median = (30 + 40) / 2 = 35
What is the Mode?
The mode is the value that appears most frequently in a dataset. A dataset can have one mode (unimodal), multiple modes (multimodal), or no mode at all if all values appear with the same frequency.
Example 1 (Single Mode): For the numbers 10, 20, 20, 30, 40:
- The number 20 appears twice, which is more than any other number.
- Mode = 20
Example 2 (Multiple Modes): For the numbers 10, 10, 20, 30, 30, 40:
- Both 10 and 30 appear twice.
- Modes = 10, 30
Example 3 (No Distinct Mode): For the numbers 10, 20, 30, 40, 50:
- Each number appears only once.
- Mode = No distinct mode
What is the Range?
The range is the difference between the highest and lowest values in a dataset. It's a simple measure of data dispersion, indicating the spread of the data.
Formula: Range = (Highest Value) – (Lowest Value)
Example: For the numbers 10, 20, 30, 40, 50:
- Highest value = 50
- Lowest value = 10
- Range = 50 – 10 = 40
When to Use Each Measure?
- Mean: Best for symmetrically distributed data without extreme outliers. Useful for calculating averages like test scores or average income in a balanced population.
- Median: Ideal for skewed distributions or data with outliers, such as housing prices or salaries, where a few very high or low values could distort the mean.
- Mode: Useful for categorical data or when you want to find the most popular item or most frequent occurrence, like the most common shoe size or favorite color.
- Range: Provides a quick and easy understanding of the spread of data, but it can be heavily influenced by a single outlier.
By using this calculator, you can quickly determine these key statistical measures for any set of numbers, aiding in data analysis and interpretation.