0
I am building a calculator, which in this specific case, the user will click a button and a simple rule equation will appear. Then it will replace the variables of the formula, which is in the input
, and after the values have been entered into the equation, the user will click again on the button and I want to make a check where if inside the input
contains a valid equation it returns the correct result. But I could not find something similar on the net about this type of error handling. Thanks for your help.
Follows the Javascript
:
function regraDeTresSimples(){
var vis = document.calcform.visor;
var resultado;
if (vis.value == "" ) {
document.calcform.visor.value = "x=(A*B)/C";
document.getElementById("explicacao").src = "img/regradetressimples.JPG";
document.getElementById("explicacao").alt = "Aprenda a calcular regra de três";
document.getElementById("explicacao").title = "Posição de cada variavel dentro da regra de três simples";
} else if(isNaN(vis.value)){
resultado = eval(vis.value);
}
vis.value = resultado;
}
Follows HTML
:
<form name="calcform" method="post" action="">
<input type="text" name="visor" id="visor" value=""/>
<input type="button" name="dividir" class="formula" value="R3s" title="Regra de Três simples" onclick="regraDeTresSimples()" />
</form>
I don’t quite understand what mistake you want to be dealt with.
– G. Bittencourt
I want to know that part of the code:
if(isNaN(vis.value))
if there is no error it can perform the functionresultado = eval(vis.value);
.– Moises Moraes