0
document.querySelector('button').addEventListener('click', calcular)
function calcular() {
peso = document.getElementById('peso').value
peso = peso.replace(",", ".")
peso = Number(peso)
altura = document.querySelector('#altura').value
altura = altura.replace(',', '.')
altura = Number(altura)
res = document.getElementById('res').value = (peso / (altura ** 2)).toFixed(2)
if (peso == '' || altura == '') {
alert('[Erro],Precisa inseri todos os campos!')
} else if (res > 0 && res < 18.5) {
alert(res)
//não está mostrando o resultado na tela
res.innerHTML = res
res.innerHTML += res.toString()
document.body.style.background = 'yellow'
} else if (res < 24.9) {
alert('oikkkk')
document.body.style.background = 'black'
} else if (res < 29.9) {
alert('oi')
//mesmo informando o res não mostra nada
res.innerText += 'oi'
}
}
<!DOCTYPE html>
<html lang="pt-BR">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Imc</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<header>
<h1>IMC</h1>
</header>
<main>
<section>
<div>
<p>Vamos calcular o imc
<button>calcular</button>
</p>
<input type="text" name="peso" id="peso" placeholder=" Exe: 95, 74.5, 30,2"> Digite seu peso
<input type="text" name="altura" id="altura" placeholder=" Exe: 1.80 , 1.7 , 2,02"> Digite sua altura
</div>
<div id="res" align='center'>
Seu resultado...
</div>
</section>
</main>
<script src="index.js"></script>
</body>
</html>
I put the Number
because I wanted to use any number even with a comma, but I got to pass the replace().
Without Number
I cannot show the error if both weight or height are not indicated, parsefloat
has to pass eval()
to show the error or inform .
achieve, did what you suggested to me and also changed another way of declaring methods and objects, thank you very much.
– Everton Costa Souza