diff --git a/Calculators/BMI-Calculator/script.js b/Calculators/BMI-Calculator/script.js index b638f58af..7cbcdbe35 100644 --- a/Calculators/BMI-Calculator/script.js +++ b/Calculators/BMI-Calculator/script.js @@ -15,19 +15,50 @@ var span = document.getElementsByClassName("close")[0]; // document.querySelector("#result").innerHTML = "00.00"; function calculate() { - if (age.value == '' || height.value == '' || weight.value == '' || (male.checked == false && female.checked == false)) { - modal.style.display = "block"; - modalText.innerHTML = 'ALL fields are required!'; - } else if (!isPositiveNumber(height.value) || !isPositiveNumber(weight.value)) { - modal.style.display = "block"; - modalText.innerHTML = 'Please enter valid positive values for height and weight!'; - } else { - countBmi(); - } + if ( + age.value == "" || + height.value == "" || + weight.value == "" || + (male.checked == false && female.checked == false) + ) { + modal.style.display = "block"; + modalText.innerHTML = "ALL fields are required!"; + } else if ( + !isPositiveNumber(height.value) || + !isPositiveNumber(weight.value) || + !isValidCombination(age.value, height.value) + ) { + modal.style.display = "block"; + modalText.innerHTML = + "Please enter valid values for age, height, and weight!"; + } else { + countBmi(); + } } function isPositiveNumber(value) { return /^\d*\.?\d+$/.test(value) && parseFloat(value) > 0; } + +function isValidCombination(age, height) { + age = parseInt(age); + height = parseFloat(height); + + if (age < 1 || age > 120) { + return false; // Age should be between 1 and 120 + } + + if (age <= 5) { + // Typical height range for age 0-5 years in cm + return height >= 60 && height <= 120; + } + else if (age <= 10) { + // Typical height range for age 6-10 years in cm + return height >= 100 && height <= 150; + } + // For adults,No restriction + return height >= 140 && height <= 220; +} + function countBmi() { var p = [age.value, height.value, weight.value]; if (male.checked) {