Advanced Z-Score Calculator
:root {
–primary: #4a6bff;
–secondary: #6c5ce7;
–success: #00b894;
–danger: #d63031;
–light: #f8f9fa;
–dark: #2d3436;
–border-radius: 8px;
–box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}
* {
box-sizing: border-box;
margin: 0;
padding: 0;
font-family: ‘Segoe UI’, system-ui, sans-serif;
}
body {
background-color: #f5f7ff;
color: var(–dark);
line-height: 1.6;
padding: 20px;
}
.calculator-container {
max-width: 900px;
margin: 0 auto;
background: white;
border-radius: var(–border-radius);
box-shadow: var(–box-shadow);
padding: 30px;
}
h1 {
color: var(–primary);
text-align: center;
margin-bottom: 25px;
font-weight: 600;
}
.tab-container {
display: flex;
margin-bottom: 20px;
border-bottom: 1px solid #ddd;
}
.tab {
padding: 12px 20px;
cursor: pointer;
font-weight: 500;
border-bottom: 3px solid transparent;
transition: all 0.3s;
}
.tab.active {
color: var(–primary);
border-bottom-color: var(–primary);
}
.tab-content {
display: none;
animation: fadeIn 0.3s ease;
}
.tab-content.active {
display: block;
}
.input-group {
margin-bottom: 15px;
}
label {
display: block;
margin-bottom: 8px;
font-weight: 500;
color: var(–primary);
}
input {
width: 100%;
padding: 12px 15px;
border: 2px solid #e0e0e0;
border-radius: var(–border-radius);
font-size: 16px;
transition: all 0.3s;
}
input:focus {
outline: none;
border-color: var(–primary);
box-shadow: 0 0 0 3px rgba(74, 107, 255, 0.1);
}
button {
padding: 12px 20px;
background-color: var(–primary);
color: white;
border: none;
border-radius: var(–border-radius);
font-size: 16px;
font-weight: 600;
cursor: pointer;
transition: all 0.3s;
margin-top: 10px;
}
button:hover {
background-color: #3a5bef;
transform: translateY(-2px);
}
button:active {
transform: translateY(0);
}
.result-section {
margin-top: 25px;
padding: 20px;
background-color: #f8f9ff;
border-radius: var(–border-radius);
border-left: 4px solid var(–primary);
display: none;
}
.result-value {
font-size: 20px;
font-weight: 700;
color: var(–primary);
margin-bottom: 15px;
}
.probability-grid {
display: grid;
grid-template-columns: repeat(2, 1fr);
gap: 15px;
margin-top: 20px;
}
.probability-card {
padding: 15px;
background: white;
border-radius: var(–border-radius);
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
}
.probability-label {
font-weight: 500;
color: #666;
margin-bottom: 5px;
}
.probability-value {
font-size: 18px;
font-weight: 600;
}
.error-message {
color: var(–danger);
font-size: 14px;
margin-top: 5px;
display: none;
}
.normal-curve {
height: 200px;
background-color: #f0f2ff;
border-radius: var(–border-radius);
margin: 20px 0;
position: relative;
overflow: hidden;
}
.curve-line {
position: absolute;
bottom: 0;
width: 100%;
height: 100%;
stroke: var(–primary);
stroke-width: 2;
fill: none;
}
.z-marker {
position: absolute;
bottom: 10px;
height: 20px;
width: 2px;
background-color: var(–danger);
}
.z-label {
position: absolute;
bottom: 35px;
font-size: 12px;
font-weight: 500;
color: var(–danger);
transform: translateX(-50%);
}
@keyframes fadeIn {
from { opacity: 0; transform: translateY(10px); }
to { opacity: 1; transform: translateY(0); }
}
@media (max-width: 768px) {
.probability-grid {
grid-template-columns: 1fr;
}
.calculator-container {
padding: 20px;
}
.tab-container {
flex-direction: column;
}
.tab {
border-bottom: 1px solid #eee;
border-left: 3px solid transparent;
}
.tab.active {
border-left-color: var(–primary);
border-bottom-color: #eee;
}
}
Z-Score Calculator
Basic Z-Score
Z-Score Converter
Probability Between
Left Bound (Z₁)
Please enter a valid number
Right Bound (Z₂)
Please enter a valid number
document.addEventListener(‘DOMContentLoaded’, function() {
// Tab switching
const tabs = document.querySelectorAll(‘.tab’);
tabs.forEach(tab => {
tab.addEventListener(‘click’, function() {
// Remove active class from all tabs
tabs.forEach(t => t.classList.remove(‘active’));
// Add active class to clicked tab
this.classList.add(‘active’);
// Hide all tab contents
document.querySelectorAll(‘.tab-content’).forEach(content => {
content.classList.remove(‘active’);
});
// Show the selected tab content
const tabId = this.getAttribute(‘data-tab’);
document.getElementById(`${tabId}-tab`).classList.add(‘active’);
});
});
// Basic Z-Score Calculation
document.getElementById(‘calculate-basic’).addEventListener(‘click’, function() {
const rawScore = parseFloat(document.getElementById(‘raw-score’).value);
const mean = parseFloat(document.getElementById(‘mean’).value);
const stdDev = parseFloat(document.getElementById(‘std-dev’).value);
// Validate inputs
let valid = true;
if (isNaN(rawScore)) {
document.getElementById(‘error-raw-score’).style.display = ‘block’;
valid = false;
} else {
document.getElementById(‘error-raw-score’).style.display = ‘none’;
}
if (isNaN(mean)) {
document.getElementById(‘error-mean’).style.display = ‘block’;
valid = false;
} else {
document.getElementById(‘error-mean’).style.display = ‘none’;
}
if (isNaN(stdDev) || stdDev <= 0) {
document.getElementById('error-std-dev').style.display = 'block';
valid = false;
} else {
document.getElementById('error-std-dev').style.display = 'none';
}
if (!valid) return;
// Calculate Z-Score
const zScore = (rawScore – mean) / stdDev;
// Display result
document.getElementById('basic-result-value').innerHTML = `
Z-Score =
${zScore.toFixed(4)}
`;
// Update normal curve
updateNormalCurve(‘basic’, zScore);
// Show explanation
document.getElementById(‘basic-explanation’).innerHTML = `
Calculation:
Z = (x – μ) / σ
Z = (${rawScore} – ${mean}) / ${stdDev}
Z = ${(rawScore – mean).toFixed(4)} / ${stdDev}
Z = ${zScore.toFixed(4)}
Interpretation:
This means the raw score of ${rawScore} is ${Math.abs(zScore).toFixed(2)} standard deviations
${zScore > 0 ? ‘above’ : ‘below’} the mean.
`;
// Show result section
document.getElementById(‘basic-result’).style.display = ‘block’;
});
// Z-Score and Probability Converter
document.getElementById(‘calculate-converter’).addEventListener(‘click’, function() {
const zScoreInput = document.getElementById(‘z-score-convert’).value;
const probInput = document.getElementById(‘probability-less’).value;
let zScore, probability;
let valid = true;
if (zScoreInput !== ”) {
zScore = parseFloat(zScoreInput);
if (isNaN(zScore)) {
document.getElementById(‘error-z-score’).style.display = ‘block’;
valid = false;
} else {
document.getElementById(‘error-z-score’).style.display = ‘none’;
document.getElementById(‘error-prob-less’).style.display = ‘none’;
// Calculate probabilities from Z-Score
probability = calculateStandardNormalProbability(zScore);
}
} else if (probInput !== ”) {
probability = parseFloat(probInput);
if (isNaN(probability) || probability 1) {
document.getElementById(‘error-prob-less’).style.display = ‘block’;
valid = false;
} else {
document.getElementById(‘error-prob-less’).style.display = ‘none’;
document.getElementById(‘error-z-score’).style.display = ‘none’;
// Calculate Z-Score from probability
zScore = calculateZScoreFromProbability(probability);
}
} else {
alert(‘Please enter either a Z-Score or Probability’);
return;
}
if (!valid) return;
updateConverterResults(zScore, probability);
});
// Probability Between Two Z-Scores
document.getElementById(‘calculate-between’).addEventListener(‘click’, function() {
const z1 = parseFloat(document.getElementById(‘z1’).value);
const z2 = parseFloat(document.getElementById(‘z2’).value);
// Validate inputs
let valid = true;
if (isNaN(z1)) {
document.getElementById(‘error-z1’).style.display = ‘block’;
valid = false;
} else {
document.getElementById(‘error-z1’).style.display = ‘none’;
}
if (isNaN(z2)) {
document.getElementById(‘error-z2’).style.display = ‘block’;
valid = false;
} else {
document.getElementById(‘error-z2’).style.display = ‘none’;
}
if (!valid) return;
// Calculate probabilities
const p1 = calculateStandardNormalProbability(z1);
const p2 = calculateStandardNormalProbability(z2);
const betweenProb = Math.abs(p2 – p1);
// Display result
document.getElementById(‘between-result-value’).innerHTML = `
P(${Math.min(z1, z2).toFixed(2)} < x < ${Math.max(z1, z2).toFixed(2)}) =
${betweenProb.toFixed(4)}
`;
// Update normal curve
updateBetweenCurve(z1, z2);
// Show explanation
document.getElementById(‘between-explanation’).innerHTML = `
Calculation:
P(x < ${z2.toFixed(2)}) = ${p2.toFixed(4)}
P(x < ${z1.toFixed(2)}) = ${p1.toFixed(4)}
P(${z1.toFixed(2)} < x < ${z2.toFixed(2)}) = |${p2.toFixed(4)} – ${p1.toFixed(4)}|
= ${betweenProb.toFixed(4)}
`;
// Show result section
document.getElementById(‘between-result’).style.display = ‘block’;
});
// Helper function to calculate standard normal probability
function calculateStandardNormalProbability(z) {
// This is a simplified approximation of the standard normal CDF
const t = 1 / (1 + 0.2316419 * Math.abs(z));
const d = 0.3989423 * Math.exp(-z * z / 2);
let p = d * t * (0.3193815 + t * (-0.3565638 + t * (1.781478 + t * (-1.821256 + t * 1.330274))));
if (z > 0) p = 1 – p;
return p;
}
// Helper function to calculate Z-Score from probability (inverse CDF)
function calculateZScoreFromProbability(p) {
// This is a simplified approximation of the inverse standard normal CDF
if (p = 1) return NaN;
const a1 = -39.6968302866538, a2 = 220.946098424521;
const a3 = -275.928510446969, a4 = 138.357751867269;
const a5 = -30.6647980661472, a6 = 2.50662827745924;
const b1 = -54.4760987982241, b2 = 161.585836858041;
const b3 = -155.698979859887, b4 = 66.8013118877197;
const b5 = -13.2806815528857;
const c1 = -0.00778489400243029, c2 = -0.322396458041136;
const c3 = -2.40075827716184, c4 = -2.54973253934373;
const c5 = 4.37466414146497, c6 = 2.93816398269878;
const d1 = 0.00778469570904146, d2 = 0.32246712907004;
const d3 = 2.445134137143, d4 = 3.75440866190742;
let q, r, z;
if (p 0.97575) {
q = Math.sqrt(-2 * Math.log(1 – p));
z = -(((((c1 * q + c2) * q + c3) * q + c4) * q + c5) * q + c6) /
((((d1 * q + d2) * q + d3) * q + d4) * q + 1);
} else {
q = p – 0.5;
r = q * q;
z = (((((a1 * r + a2) * r + a3) * r + a4) * r + a5) * r + a6) * q /
(((((b1 * r + b2) * r + b3) * r + b4) * r + b5) * r + 1);
}
return z;
}
// Update converter results
function updateConverterResults(zScore, pLess) {
document.getElementById(‘converter-result-value’).innerHTML = `
Z-Score =
${zScore.toFixed(4)}
`;
document.getElementById(‘p-less’).textContent = pLess.toFixed(6);
document.getElementById(‘p-greater’).textContent = (1 – pLess).toFixed(6);
document.getElementById(‘p-to-zero’).textContent = Math.abs(pLess – 0.5).toFixed(6);
document.getElementById(‘p-between’).textContent = (Math.abs(2 * pLess – 1)).toFixed(6);
document.getElementById(‘p-outside’).textContent = (2 * Math.min(pLess, 1 – pLess)).toFixed(6);
// Update normal curve
updateNormalCurve(‘converter’, zScore);
// Show result section
document.getElementById(‘converter-result’).style.display = ‘block’;
}
// Update normal curve visualization
function updateNormalCurve(type, zScore) {
const container = document.getElementById(`${type}-curve`);
const marker = document.getElementById(`${type}-marker`);
const label = document.getElementById(`${type}-label`);
const containerWidth = container.offsetWidth;
const center = containerWidth / 2;
const scale = containerWidth / 6; // Scale factor for z-scores
// Position marker
const markerPos = center + (zScore * scale);
marker.style.left = `${markerPos}px`;
label.style.left = `${markerPos}px`;
label.textContent = `Z = ${zScore.toFixed(2)}`;
// Color marker based on position
if (zScore > 1.5) {
marker.style.backgroundColor = ‘var(–success)’;
} else if (zScore < -1.5) {
marker.style.backgroundColor = 'var(–danger)';
} else {
marker.style.backgroundColor = 'var(–primary)';
}
}
// Update between curve visualization
function updateBetweenCurve(z1, z2) {
const container = document.getElementById('between-curve');
const leftMarker = document.getElementById('left-marker');
const leftLabel = document.getElementById('left-label');
const rightMarker = document.getElementById('right-marker');
const rightLabel = document.getElementById('right-label');
const betweenArea = document.getElementById('between-area');
const containerWidth = container.offsetWidth;
const center = containerWidth / 2;
const scale = containerWidth / 6; // Scale factor for z-scores
// Position markers
const leftPos = center + (Math.min(z1, z2) * scale);
const rightPos = center + (Math.max(z1, z2) * scale);
leftMarker.style.left = `${leftPos}px`;
leftLabel.style.left = `${leftPos}px`;
leftLabel.textContent = `Z = ${Math.min(z1, z2).toFixed(2)}`;
rightMarker.style.left = `${rightPos}px`;
rightLabel.style.left = `${rightPos}px`;
rightLabel.textContent = `Z = ${Math.max(z1, z2).toFixed(2)}`;
// Highlight area between
betweenArea.style.left = `${leftPos}px`;
betweenArea.style.width = `${rightPos – leftPos}px`;
}
});
Like this:
Like Loading...
Related