function validateInputBt(inputElement, min, max) { const value = parseFloat(inputElement.value); if (value < min || value > max) { alert(`The value must be a number between ${min} y ${max}.`); inputElement.value = ''; } } function factorial(n) { if (n === 0 || n === 1) { return 1; } let result = 1; for (let i = 2; i <= n; i++) { result *= i; } return result; } function binomialProbability() { const totBt = parseInt(document.getElementById("totBt").value); const pattBt = parseInt(document.getElementById("pattBt").value); const avgBt = parseFloat(document.getElementById("avgBt").value); if (isNaN(totBt) || isNaN(pattBt) || isNaN(avgBt) || totBt < 0 || pattBt < 0 || avgBt < 0) { document.getElementById("probBt").textContent = "0.0"; return; } const p = avgBt / 100; let probability = 0; for (let k = 0; k < pattBt; k++) { const coefficient = factorial(totBt) / (factorial(k) * factorial(totBt - k)); const term = coefficient * Math.pow(p, k) * Math.pow(1 - p, totBt - k); probability += term; } document.getElementById("probBt").textContent = ((1 - probability) * 100).toFixed(2); } const totBt = document.getElementById('totBt'); const pattBt = document.getElementById('pattBt'); const avgBt = document.getElementById('avgBt'); const totalMin = 1, totalMax = 170; const patternMin = 0, patternMax = 170; const avgMin = 0, avgMax = 100; totBt.addEventListener('input', () => validateInputBt(totBt, totalMin, totalMax)); pattBt.addEventListener('input', () => validateInputBt(pattBt, patternMin, patternMax)); avgBt.addEventListener('input', () => validateInputBt(avgBt, avgMin, avgMax));