Catch the value of an input and return alert

1

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);

}

    
asked by anonymous 14.01.2018 / 14:00

2 answers

1

The problem is in alert :

alert(ok);

Where ok is not a variable. The correct would be to enclose it in quotation marks, single or double:

alert("ok"); or alert('ok');

When you get to this ready your script does not proceed because it gives undefined variable error.

Complementing , when you put a word in the code without quotation marks, the code assumes that it is an object (variable, function ...) and not a text. Objects must be declared, otherwise results in error.

    
14.01.2018 / 14:29
1

The above answer is correct, but apparently you have an error in calculating BMI, take a look at it. Since I had already started to answer the question I will publish a code that I had made based on yours.

function calcularimc() {
   var quilos = parseInt(document.getElementById("quilos").value);
   var metros = parseInt(document.getElementById("metros").value);
   var centimetros = parseInt(document.getElementById("centimetros").value);

   var altura = (metros*100 + centimetros)/100;
   var imc = quilos / (altura * altura);
   console.log(imc);
   if(imc <= 18.5){
      document.getElementById("resultado").innerHTML = 'Abaixo do peso';
   }else if(imc <= 24.9){
      document.getElementById("resultado").innerHTML = 'Peso normal';
   }else if(imc <= 29.9){
      document.getElementById("resultado").innerHTML = 'Peso em excesso';
   }else if(imc > 29.9){
      document.getElementById("resultado").innerHTML = 'Obesidade';
   }
}
<form id="formulario">
  <input type="text" id="quilos" placeholder="Quilos">
  <input type="text" id="metros" placeholder="Metros">
  <input type="text" id="centimetros" placeholder="Centímetros">
  <button type="button" onclick="calcularimc()">Calcular</button>
  <br><br>
  <div id="resultado"></div>
</form>
    
14.01.2018 / 14:34