2
Good morning!
I am studying programming logic with JS and would like to know how to ask the user to type the operation, so the JS interprets this and generates the result...
<script>
var n1 = prompt('Digite um número');
n1 = parseInt(n1);
var n2 = prompt('Digite outro número');
n2 = parseInt(n2);
var operacao = prompt('Digite a operação:')
var resultado = (n1+operacao+n2);
alert("O resultado da mutiplicação é: " + resultado)
</script>
Thanks in advance
For this didactic exercise, the advised
var resultado = eval((n1+operacao+n2));
solves, but for better learning, you would have to create a function that checks the operator and whether n1 and N2 were correctly reported as numbers to get the expected result, with N2 plus checking not being equal to zero in the split operation.– Benilson
This answers your question? Create function to make calculations with a given operator using Javascript
– Luiz Felipe