CALCULATOR
21 October 2024 at 13:01
<html> <head> <title>Simple Calculator</title> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="author" content="Sugirtha"> <meta name="description" content="Calculator using HTML, CSS, JavaScript"> <link rel="stylesheet" href="calc.css"> </head> <body> <h2 align="center">CALCULATOR</h2> <div class="borderContainer"> <div class="container" > <div class="resultsContainer" ><input type="text" id="results" name="results" class="result" readonly></div> <div class="numPadContainer"> <p> <input type="button" value="7" class="button black" onclick='calc("7")'> <input type="button" value="8" class="button black" onclick='calc("8")'> <input type="button" value="9" class="button black" onclick='calc("9")'> <input type="button" value="/" class="button opr" onclick='calc("/")'> </p> <p> <input type="button" value="4" class="button black" onclick='calc("4")'> <input type="button" value="5" class="button black" onclick='calc("5")'> <input type="button" value="6" class="button black" onclick='calc("6")'> <input type="button" value="*" class="button opr" onclick='calc("*")'> </p> <p> <input type="button" value="1" class="button black" onclick='calc("1")'> <input type="button" value="2" class="button black" onclick='calc("2")'> <input type="button" value="3" class="button black" onclick='calc("3")'> <input type="button" value="-" class="button opr" onclick='calc("-")'> </p> <p> <input type="button" value="0" class="button black" onclick='calc("0")'> <input type="button" value="00" class="button black" onclick='calc("00")'> <input type="button" value="." class="button black" onclick='calc(".")'> <input type="button" value="+" class="button opr" onclick='calc("+")'> </p> <p> <input type="button" value="mod" class="button opr" onclick='calc("%")'> <input type="button" value="C" class="button red" onclick='results.value=""'> <input type="button" value="=" class="button orange" onclick='doOper()' > </p> </div> </div> </div> <script> var answered=false; function calc(val) { optrs = "+-*/%" if (answered && !optrs.includes(val)) { //alert("not included opr - clearing ans") document.getElementById("results").value=""; } document.getElementById("results").value += val; answered=false; } function doOper() { try { dispAns(eval(document.getElementById("results").value)); } catch { dispAns("Input Error"); } } function dispAns(output) { document.getElementById("results").value = output; answered=true; } </script> </body> </html>
body { background-color:black; color:lightgrey; } .borderContainer { position:relative; top:10px; left:36%; width:370; height:500; background-color: lightgrey; } .container { position:relative; left:8%; top:25px; width:310px; height:450px; background-color:#3d4543; border-radius:3px; } .numPadContainer { position:relative; left:25px; top:40px; } .result { position:relative; left:24px; top:28px; background-color:#bccd95; color:black; text-align:right; height:40px; width:260px; border-radius:10px; border-color:white; } .button { position:relative; color:white; border:none; border-radius:8px; cursor:pointer; width:48px; height:42px; margin-left:10px; } .button.black { background-color:#303030; border-radius:8px; border-bottom:black 2px solid; border-top:2px 303030 solid; } .button.red { background-color:#cf1917; border-bottom:black 2px solid; font-weight:bold; } .button.opr { background-color:grey; border-bottom:black 2px solid; } .button.orange { background-color:orange; border-bottom:black 2px solid; width:110px; font-weight:bold; }
OUTPUT: