Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>Calculator</title>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- </head>
- <body style="background-color: black;">
- <style>
- .txt {
- color: white;
- font-family: arial;
- }
- </style>
- <h1 class="txt">Enter Operand.</h1>
- <input id="operand" value="">
- <h1 class="txt">Enter your first number.</h1>
- <input id="num1" value="">
- <h1 class="txt">Enter your second number.</h1>
- <input id="num2" value="">
- <h1 class="txt" id="sum"></h1>
- <script>
- const operandInp = document.getElementById("operand");
- const num1 = document.getElementById("num1");
- const num2 = document.getElementById("num2");
- const cout = document.getElementById("sum");
- var p1;
- var p2;
- var sum;
- function entered(key) {
- if (key.keyCode == 13) {
- if (operandInp.value == "+") { //Add formula
- p1 = Number(num1.value);
- p2 = Number(num2.value);
- sum = p1 + p2;
- cout.innerHTML = "The sum is " + String(sum);
- }
- if (operandInp.value == "-") { //Subtract formula
- p1 = Number(num1.value);
- p2 = Number(num2.value);
- sum = p1 - p2;
- cout.innerHTML = "The sum is " + String(sum);
- }
- if (operandInp.value == "*") { //Multiply formula
- p1 = Number(num1.value);
- p2 = Number(num2.value);
- sum = p1 * p2;
- cout.innerHTML = "The sum is " + String(sum);
- }
- if (operandInp.value == "/") { //Divide formula
- p1 = Number(num1.value);
- p2 = Number(num2.value);
- sum = p1 / p2;
- cout.innerHTML = "The sum is " + String(sum);
- }
- }
- }
- document.onkeydown = entered;
- </script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement