Take the value of an input and return alert

Asked

Viewed 1,095 times

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 if ok 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?

  • 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!

2 answers

1


The problem is in alert:

alert(ok);

Where ok is not a variable. The correct thing would be to put it between quotes, single or double:

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

When arriving at this ready your script does not proceed because it gives error of undefined variable.

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.

  • Ball show, that was it. Thank you very much!

1

The previous answer is correct, but apparently there is an error in the calculation of the BMI, take a look at this. As I had already begun to answer the question I will publish a code I had made based on your.

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>

  • Phelipe, thank you! I really forgot the quotes. Problems of beginner hehe

  • Ahh, Phelipe. In this case, I added an if Else later to show a new alert for a different weight. It worked, but I can’t add another one. Can you tell me the problem?

  • Haron, I made an edit on my reply by arranging the code to do the calculation correctly and already show the value on the screen. I was not calculating correctly because I was understanding centimeters as a string and was concatenating with height, and not adding.

  • Phelipe, is that show the result of the calculation is not really the goal of my alert, but rather take the information of the calculated value, make a comparative and inform the individual whether he is overweight or not, depending on the level. You want to see all the code so you can understand better?

  • I edited again, setting the conditions, but if that’s not the way you wanted it, just take the IMC variable and do what your project needs with it.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.