0
Calculator
<style type="text/css">
#div1{
text-align:center;
border:1px solid black;
width:20%;
height:200px;
position:absolute;
left:40%;
}
#campo1,#campo2{
width:40px;
margin:5px;
}
#btn1,#btn2, #btn3, #btn4{
margin:3px;
}
#result{
float:center;
}
</style>
<script language="javascript">
var num1 = parseInt(document.getElementById("campo1").value);
var num2 = parseInt(document.getElementById("campo2").value);
var soma = (num1+num2);
var subtracao = (num1-num2);
var multiplicacao = (num1*num2);
var divisao = (num1/num2);
function somar(){
if(isNaN(num1) || isNaN(num2)){
alert("Os campos devem conter apenas numeros");
}
else{
document.getElementByID("valorFinal").innerHTML = soma;
}
}
function subtrair(){
if(isNaN(num1) || isNaN(num2){
alert("Os campos devem conter apenas numeros");
}
else{
document.getElementByID("valorFinal").innerHTML = subtracao;
}
}
function multiplicar(){
if(isNaN(num1) || isNaN(num2){
alert("Os campos devem conter apenas numeros");
}
else{
document.getElementByID("valorFinal").innerHTML = multiplicao;
}
}
function dividir(){
if(isNaN(num1) || isNaN(num2){
alert("Os campos devem conter apenas numeros");
}
else{
document.getElementByID("valorFinal").innerHTML = divisao;
}
}
</script>
</head>
<body>
<div id="div1">
<h1>Calculadora</h1>
<input type="text" id="campo1" />
<input type="text" id="campo2" /><br />
<input type="button" value="+" id="btn1" onClick="somar()">
<input type="button" value="-" id="btn2" onClick="subtrair()">
<input type="button" value="*" id="btn3" onClick="multiplicar()">
<input type="button" value="/" id="btn4" onClick="dividir()"><br />
<b><label id="result">Resultado: </label></b>
<p id="valorFinal"></p>
</div>
</body>
Welcome to Stack Overflow in English. This code may be a solution to the question, but your answer might be better if you [Edit] and include an explanation of the key points of the code. The goal Code-only answers - What to do?.
– Icaro Martins