1 Answers
π Understanding Average (Mean) in JavaScript
The average, or mean, is the sum of all values divided by the number of values. It represents the central tendency of a dataset. Here's how to calculate it using JavaScript:
- β First, calculate the sum of all numbers in the array.
- β Then, divide the sum by the total number of elements in the array.
function calculateAverage(arr) {
if (arr.length === 0) {
return 0; // Avoid division by zero for empty arrays
}
const sum = arr.reduce((acc, num) => acc + num, 0);
return sum / arr.length;
}
const numbers = [1, 2, 3, 4, 5];
const average = calculateAverage(numbers);
console.log("Average:", average); // Output: Average: 3
π Understanding Median (Middle Value) in JavaScript
The median is the middle value in a sorted dataset. If there are an even number of values, the median is the average of the two middle values. Calculating the median requires sorting the array first. Here's the JavaScript code:
- π§Ί Sort the array in ascending order.
- π If the array length is odd, the median is the middle element.
- β If the array length is even, the median is the average of the two middle elements.
function calculateMedian(arr) {
const sortedArr = [...arr].sort((a, b) => a - b); // Create a sorted copy
const middleIndex = Math.floor(sortedArr.length / 2);
if (sortedArr.length % 2 === 0) {
// Even number of elements
return (sortedArr[middleIndex - 1] + sortedArr[middleIndex]) / 2;
} else {
// Odd number of elements
return sortedArr[middleIndex];
}
}
const numbers = [5, 2, 1, 3, 4];
const median = calculateMedian(numbers);
console.log("Median:", median); // Output: Median: 3
π’ Understanding Mode (Most Frequent Value) in JavaScript
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 only once). Here's the JavaScript code to find the mode:
- πΊοΈ Create an object (or map) to store the frequency of each number.
- π Iterate through the array, updating the frequency count for each number.
- π Find the number(s) with the highest frequency.
function calculateMode(arr) {
const frequencyMap = {};
let maxFrequency = 0;
let modes = [];
for (const num of arr) {
frequencyMap[num] = (frequencyMap[num] || 0) + 1;
if (frequencyMap[num] > maxFrequency) {
maxFrequency = frequencyMap[num];
modes = [num]; // New mode found, reset the array
} else if (frequencyMap[num] === maxFrequency) {
modes.push(num); // Another mode found
}
}
// If all numbers appear only once, there's no mode
if (modes.length === Object.keys(frequencyMap).length && modes.length === arr.length) {
return []; //No mode exists
}
return modes;
}
const numbers = [1, 2, 2, 3, 4, 2, 5];
const mode = calculateMode(numbers);
console.log("Mode:", mode); // Output: Mode: [2]
π§ͺ Practice Quiz
Test your knowledge with these practice questions! (Answers are at the end)
- What is the average of the numbers [10, 20, 30, 40, 50]?
- What is the median of the numbers [15, 5, 20, 10, 25]?
- What is the mode of the numbers [3, 5, 2, 5, 3, 3, 1]?
- What is the average of the numbers [2, 4, 6, 8]?
- What is the median of the numbers [1, 2, 3, 4, 5, 6]?
- What is the mode of the numbers [7, 8, 9, 7, 8, 7]?
- What is the average of an empty array?
β Answers to Practice Quiz
- 30
- 15
- 3
- 5
- 3.5
- 7
- 0
Join the discussion
Please log in to post your answer.
Log InEarn 2 Points for answering. If your answer is selected as the best, you'll get +20 Points! π