EJERCICIO IMC
HTML
<!DOCTYPE
html>
<html
lang="es">
<head>
<meta charset="UTF-8">
<link rel="stylesheet"
href="estilos.css">
<title>Calculadora IMC -
Bachillerato</title>
</head>
<body>
<div
class="contenedor">
<h1>Calculadora de IMC</h1>
<input type="number"
id="peso" placeholder="Peso en kg">
<input type="number"
id="altura" placeholder="Altura en cm">
<button
onclick="calcularIMC()">Calcular Resultado</button>
<div
id="resultado">Ingrese sus datos para comenzar</div>
</div>
<script
src="script.js"></script>
</body>
</html>
CSS
body {
background-color: #f4f7f6;
font-family: 'Arial', sans-serif;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
.contenedor
{
background: white;
padding: 30px;
border-radius: 12px;
box-shadow: 0 4px 15px rgba(0,0,0,0.1);
text-align: center;
width: 300px;
}
input {
width: 90%;
padding: 10px;
margin: 10px 0;
border: 1px solid #ddd;
border-radius: 5px;
}
button {
background-color: #3498db;
color: white;
border: none;
padding: 12px;
width: 100%;
border-radius: 5px;
cursor: pointer;
}
#resultado
{
margin-top: 20px;
padding: 15px;
border-radius: 5px;
font-weight: bold;
}
/* Clases
dinámicas */
.verde {
background-color: #2ecc71; color: white; }
.amarillo {
background-color: #f1c40f; color: black; }
.rojo {
background-color: #e74c3c; color: white; }
JS
function
calcularIMC() {
const peso =
parseFloat(document.getElementById('peso').value);
const alturaCm =
parseFloat(document.getElementById('altura').value);
const divResultado =
document.getElementById('resultado');
if (peso > 0 && alturaCm > 0) {
const alturaM = alturaCm / 100;
const imc = (peso / (alturaM *
alturaM)).toFixed(1);
let mensaje = "";
let clase = "";
if (imc < 18.5) {
mensaje = `IMC: ${imc} -
Bajo Peso`;
clase = "amarillo";
} else if (imc >= 18.5 &&
imc <= 24.9) {
mensaje = `IMC: ${imc} -
Peso Normal`;
clase =
"verde";
} else {
mensaje =
`IMC: ${imc} - Sobrepeso`;
clase =
"rojo";
}
divResultado.innerHTML = mensaje;
divResultado.className = clase; // Aplica la clase de CSS dinámicamente
} else {
divResultado.innerHTML = "Por favor, llene los campos
correctamente";
divResultado.className =
"";
}
}
Comentarios
Publicar un comentario