Advertisement
Guest User

simple calculator on the web

a guest
Nov 13th, 2019
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 2.60 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.     <head>
  4.         <title>Calculator</title>
  5.         <meta charset="UTF-8">
  6.         <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7.     </head>
  8.         <body style="background-color: black;">
  9.             <style>
  10.                 .txt {
  11.                     color: white;
  12.                     font-family: arial;
  13.                 }
  14.             </style>
  15.             <h1 class="txt">Enter Operand.</h1>
  16.             <input id="operand" value="">
  17.             <h1 class="txt">Enter your first number.</h1>
  18.             <input id="num1" value="">
  19.             <h1 class="txt">Enter your second number.</h1>
  20.             <input id="num2" value="">
  21.             <h1 class="txt" id="sum"></h1>
  22.             <script>
  23.                 const operandInp = document.getElementById("operand");
  24.                 const num1 = document.getElementById("num1");
  25.                 const num2 = document.getElementById("num2");
  26.                 const cout = document.getElementById("sum");
  27.                 var p1;
  28.                 var p2;
  29.                 var sum;
  30.  
  31.                 function entered(key) {
  32.                     if (key.keyCode == 13) {
  33.                         if (operandInp.value == "+") { //Add formula
  34.                             p1 = Number(num1.value);
  35.                             p2 = Number(num2.value);
  36.                             sum = p1 + p2;
  37.                             cout.innerHTML = "The sum is " + String(sum);
  38.                         }
  39.                         if (operandInp.value == "-") { //Subtract formula
  40.                             p1 = Number(num1.value);
  41.                             p2 = Number(num2.value);
  42.                             sum = p1 - p2;
  43.                             cout.innerHTML = "The sum is " + String(sum);
  44.                         }
  45.                         if (operandInp.value == "*") { //Multiply formula
  46.                             p1 = Number(num1.value);
  47.                             p2 = Number(num2.value);
  48.                             sum = p1 * p2;
  49.                             cout.innerHTML = "The sum is " + String(sum);
  50.                         }
  51.                         if (operandInp.value == "/") { //Divide formula
  52.                             p1 = Number(num1.value);
  53.                             p2 = Number(num2.value);
  54.                             sum = p1 / p2;
  55.                             cout.innerHTML = "The sum is " + String(sum);
  56.                         }
  57.                     }
  58.                 }
  59.                
  60.                 document.onkeydown = entered;
  61.             </script>
  62.     </body>
  63. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement