I'm trying to return a value from an input that gets the value of a variable that also gets the value of a small equation, but I'm not getting it.
The code looks 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 remove the part of if
, it returns the calculation and returns the result.
Personally, the first error was that I had not left a quoted string. Second, is that I needed to put multiple if and else if conditions. It looks 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 calculateimc () { var form = document.getElementById ('form');
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);
}