Java basic script

1

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

I did not get the logic very well, could anyone 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);

}
    
asked by anonymous 04.10.2016 / 15:36

1 answer

0

I do not know if I understood your question very well, but if it is what I realized it can do as follows:

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;

 }

}
    
04.10.2016 / 16:31