Skip to content

Commit

Permalink
added:limit for age in BMI
Browse files Browse the repository at this point in the history
  • Loading branch information
afreenpoly committed Aug 30, 2024
1 parent 8bb7f02 commit 900fbe8
Showing 1 changed file with 40 additions and 9 deletions.
49 changes: 40 additions & 9 deletions Calculators/BMI-Calculator/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 900fbe8

Please sign in to comment.