Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 12 additions & 23 deletions BMICalculator/phibersoft/app.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,26 @@
const weightInput = document.getElementById('weight');
const heightInput = document.getElementById('height');
const resultElement = document.getElementById('result');
const bmiElement = document.getElementById('bmi');
const statusElement = document.getElementById('status');

const showResults = () => {
resultElement.style.display = 'block';
}

const hideResults = () => {
resultElement.style.display = 'none';
}

hideResults();

const calculateBMI = () => {
const weight = weightInput.value;
const height = heightInput.value;
if(weight && height){

if (weight && height) {

// ✅ NEW VALIDATION (YOUR CONTRIBUTION)
if (weight <= 0 || height <= 0) {
bmiElement.innerHTML = '';
statusElement.innerHTML = '';
hideResults();
return;
}

const bmi = weight / (height ** 2);

if(isNaN(bmi)) {
if (isNaN(bmi)) {
hideResults();
return;
}

bmiElement.innerText = bmi.toFixed(2);


if (bmi < 18.5) {
statusElement.innerText = 'Underweight';
} else if (bmi < 25) {
Expand All @@ -45,7 +38,3 @@ const calculateBMI = () => {
hideResults();
}
}

weightInput.addEventListener('input', calculateBMI);
heightInput.addEventListener('input', calculateBMI);