BMI Calculator
/* styles.css */
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
padding: 0;
}
.container {
text-align: center;
}
h1 {
color: #333;
}
#calculator {
padding: 20px;
background-color: #fff;
border-radius: 5px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
label {
font-size: 16px;
margin-right: 10px;
}
input[type="number"] {
padding: 10px;
font-size: 16px;
border-radius: 5px;
border: 1px solid #ccc;
margin-right: 10px;
}
button {
background-color: #3498db;
color: #fff;
border: none;
padding: 10px 20px;
font-size: 16px;
cursor: pointer;
border-radius: 5px;
margin-top: 10px;
}
#result {
font-size: 24px;
margin-top: 10px;
}
// scripts.js
function calculateBMI() {
const height = parseFloat(document.getElementById('height').value);
const weight = parseFloat(document.getElementById('weight').value);
if (isNaN(height) || isNaN(weight)) {
alert('Please enter valid values for height and weight.');
return;
}
const bmi = (weight / ((height / 100) ** 2)).toFixed(2);
let category;
if (bmi < 18.5) {
category = 'Underweight';
} else if (bmi < 24.9) {
category = 'Normal weight';
} else if (bmi < 29.9) {
category = 'Overweight';
} else {
category = 'Obese';
}
document.getElementById('result').textContent = `Your BMI is ${bmi}. You are ${category}.`;
}
No comments:
Post a Comment