1
I’m trying to return a value from an input that receives the value of a variable that also receives the value of a small equation, but I’m not getting it.
The code is like this:
function calcularimc() {
var formulario = document.getElementById('formulario');
var quilos = +formulario.quilos.value;
var metros = +formulario.metros.value;
var centimetros = +formulario.centimetros.value;
var altura = (metros * 100 + centimetros)/100;
var imc = quilos / (altura * altura);
if (imc <= 25) {
alert(ok);
}
formulario.imc.value = imc.toFixed(2);
}
]
Obs: when I withdraw the part from the if
, it returns to do the calculation and present the result.
Guys, the first mistake was that I didn’t leave a string in quotes. Second, is that I needed to put several conditions if and if Else. Stayed like this:
<form id="formulario">
<fieldset>
<legend>Cálculo do IMC</legend>
<label for="quilos">Quilos:</label>
<input type="text" name="quilos" />
<label for="metros">Metros:</label>
<input type="text" name="metros" />
<label for="centimetros">Centímetros:</label>
<input type="text" name="centimetros" />
<label for="imc">IMC:</label>
<input type="text" name="imc" disabled="disabled" />
<a href="#" onclick="calcularimc();">calcular</a>
</fieldset>
</form>
Function calcularimc() { var formulario = Document.getElementById('formulario');
var quilos = +formulario.quilos.value;
var metros = +formulario.metros.value;
var centimetros = +formulario.centimetros.value;
var altura = (metros * 100 + centimetros)/100;
var imc = quilos / (altura * altura);
if (imc <= 18.5 ) {
alert('Abaixo do peso!');
} else if (imc > 18.6 && imc < 24.9) {
alert('Peso ideal');
} else if (imc >= 25 && imc < 29.9){
alert('Levemente acima');
} else if (imc > 30 && imc < 34.9) {
alert('Obesidade grau 1');
} else if (imc > 35 && imc < 39.9) {
alert('Obesidade grau 2');
} else if (imc > 40) {
alert('Obesidade grau 3');
}
formulario.imc.value = imc.toFixed(2);
}
Your question is a little confused.
alert(ok)
will only work ifok
is a variable. If it is supposed to appear as text it has to carry single or double quotes. What exactly is the difficulty you are finding?– Isac
Great Isac, I’m sorry, man, really my question got confused, because I’m very beginner. Really missing only the quotes in the string. Thank you very much!
– Haron Maia