Calculadora javascript

Crear en html


<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width">
    <title>operaciones basicas</title>
  </head>
  <body>

    calculadora <br>
 
      ingrese primer numero <br>
      <input type="text" name="num1" id="num1" />
      <select name="ope" id="ope">
        <option value="+">+</option>
        <option value="-">-</option>
        <option value="*">*</option>
        <option value="/">/</option>
      </select>
  <br> ingrese el segundo numero <br>
  <input type="text" name="num2" id="num2"/><br>
  <input type="text" name="res" id="res"/>
  <input type="reset" value="Borrar"/>
  <input type="button" onclick="operar();" value="Calcular ya" />
 </body>
 <script type="text/javascript" src="ope.js"></script>
</html>

archivo en javascript: "ope.js"


function operar()
    { var n1=document.getElementById('num1').value;
 var n2=document.getElementById('num2').value;
 var signo=document.getElementById('ope').value;
 switch(signo)
      { case "+": var suma=parseInt (n1) +parseInt(n2);
             res.value=suma;break;
case "-": var suma=parseInt (n1) -parseInt(n2);
             res.value=suma;break;
case "*": var suma=parseInt (n1) *parseInt(n2);
             res.value=suma;break;
case "/": var suma=parseInt (n1) /parseInt(n2);
             res.value=suma;break;
 
  }
}

Share this

Related Posts

Previous
Next Post »