Java basic script

Asked

Viewed 91 times

1

I would like to check my code, an "if Else", that when I do not put anything to calculate and click the button, it gives an alert warning that there is nothing in the value fields.

I didn’t quite grasp the logic, someone could help me?

document.getElementById("btn-calcular").onclick = function(){

    var valorA = document.getElementById("valor-a").value;
    var valorB = document.getElementById("valor-b").value;

    alert(parseInt(valorA) + parseInt(valorB));

    document.getElementById("resposta").value = parseInt(valorA) + parseInt(valorB);

}

1 answer

0

I don’t know if I understand your question very well, but if it’s what I understand you can do it this way:

document.getElementById("btn-calcular").onclick = function(){

var valorA = document.getElementById("valor-a").value;
var valorB = document.getElementById("valor-b").value;

if (valorA == "" || valorB == ""){
   alert("Por favor introduza os dados necessários");
}
else{
    alert(parseInt(valorA) + parseInt(valorB));

 //aqui ja nao precisa de usar o parseInt, uma vez que ja converteu os valores para inteiros acima

    document.getElementById("resposta").value = valorA + valorB;

 }

}
  • 1

    Man, that’s what it was. Thank you for real! Hug

  • Thanks ;) Be strong there, practice a lot!

Browser other questions tagged

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