HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="indice.css">
<title>Document</title>
</head>
<body>
<div class="container">
<h1>body mass index</h1>
<form>
<div class="formulario">
<label for="name">Nombre</label>
<input type="text" id="name" placeholder="Ingrese su nombre" />
<label for="altura">Estatura</label>
<input type="number" id="altura" placeholder="Ingrese su estastura" />
<label for="peso">Peso</label>
<input type="text" id="peso" placeholder="Ingrese su peso" />
</div>
<button class="enviar">Enviar</button>
</form>
<script src="index.js"></script>
</div>
</body>
</html>
CSS
html,
body {
margin: 0;
box-sizing: border-box;
padding: 0;
font-family: sans-serif;
display: flex;
justify-content: center;
background-color: rgb(112, 121, 120);
}
.container {
margin-top: 200px;
width: 400px;
background: radial-gradient(
circle,
rgba(224, 236, 254, 1) 0%,
rgba(138, 43, 226, 1) 100%
);
padding: 50px;
border-radius: 10px;
box-shadow: 5px 5px 5px rgba(0, 0, 0, 0.411);
}
h1 {
text-align: center;
color: white;
}
.formulario {
display: flex;
flex-direction: column;
color: white;
}
.formulario label{
font-weight: 700
}
.formulario input {
margin: 10px 0;
padding: 7px 0;
border: none;
border-radius: 6px;
}
.enviar{
padding: 10px;
font-size: 16px;
background-color: white;
border: none;
width: 100px;
margin-top: 20px;
border-radius: 7px;
}
.description{
color: white;
line-height: 25px;
font-size: 1em;
font-weight: 700;
}
JS
let name = document.querySelector("#name");
let altura = document.querySelector("#altura");
let peso = document.querySelector("#peso");
let enviar = document.querySelector(".enviar");
let container = document.querySelector(".container");
function index(peso, estatura) {
let imc = peso / (estatura * estatura);
return imc;
}
function print(name, imc) {
let div = document.createElement("div");
let p = document.createElement("p");
p.textContent = `El indice de masa corporal de ${name} es: ${imc}`;
div.className = "description";
div.appendChild(p);
container.appendChild(div);
if (imc > 18.5 && imc < 24.9) {
container.appendChild(div);
setTimeout(() => {
container.removeChild(div);
}, 3000);
clear();
} else if (imc > 25 && imc < 29.9) {
container.appendChild(div);
setTimeout(() => {
container.removeChild(div);
}, 3000);
clear();
} else if (imc < 18.5) {
container.appendChild(div);
setTimeout(() => {
container.removeChild(div);
}, 3000);
clear();
} else if (imc > 30) {
container.appendChild(div);
setTimeout(() => {
container.removeChild(div);
}, 3000);
clear();
}
}
listenerEvent();
function listenerEvent() {
enviar.addEventListener("click", enviarData);
}
function enviarData(e) {
e.preventDefault();
if (name.value && altura.value && peso.value) {
let nom = name.value;
let alt = parseFloat(altura.value);
let p = parseInt(peso.value);
let imc = index(p, alt);
print(nom, imc);
}
}
function clear() {
name.value = "";
altura.value = "";
peso.value = "";
}