function calculateGrades() {
// Get the scores from the input field
var scoresInput = document.getElementById(‘studentScores’).value;
var scores = scoresInput.split(‘,’).map(function(item) {
return parseFloat(item.trim());
});
// Validate if scores are entered correctly
if (scores.length === 0 || scores.some(isNaN)) {
alert(“Please enter valid numerical scores.”);
return;
}
// Calculate the mean and standard deviation
var mean = calculateMean(scores);
var stdDev = calculateStandardDeviation(scores, mean);
// Calculate grades based on the bell curve
var gradeResults = assignGrades(scores, mean, stdDev);
// Display results
document.getElementById(‘gradeResults’).innerHTML = `
Mean Score: ${mean.toFixed(2)}
Standard Deviation: ${stdDev.toFixed(2)}
Grade Distribution:
${gradeResults.map(result => `
${result.name}: ${result.count} students
`).join(”)}
`;
document.getElementById(‘resultsSection’).style.display = ‘block’;
}
// Function to calculate the mean
function calculateMean(scores) {
var sum = scores.reduce(function(acc, score) {
return acc + score;
}, 0);
return sum / scores.length;
}
// Function to calculate the standard deviation
function calculateStandardDeviation(scores, mean) {
var squaredDifferences = scores.map(function(score) {
return Math.pow(score – mean, 2);
});
var avgSquaredDifference = calculateMean(squaredDifferences);
return Math.sqrt(avgSquaredDifference);
}
// Function to assign grades based on the bell curve
function assignGrades(scores, mean, stdDev) {
var gradeDistribution = [
{ name: ‘A’, count: 0, threshold: mean + 1.5 * stdDev },
{ name: ‘B’, count: 0, threshold: mean + 0.5 * stdDev },
{ name: ‘C’, count: 0, threshold: mean – 0.5 * stdDev },
{ name: ‘D’, count: 0, threshold: mean – 1.5 * stdDev },
{ name: ‘F’, count: 0, threshold: -Infinity }
];
// Assign grades
scores.forEach(function(score) {
for (var i = 0; i = gradeDistribution[i].threshold) {
gradeDistribution[i].count++;
break;
}
}
});
return gradeDistribution;
}