Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 3.95 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3.     <head>
  4.         <script type="text/javascript">
  5.             let result = '0';
  6.             let empty = true;
  7.  
  8.             function update() {
  9.                 document.getElementById('calculation-result').innerHTML = result;
  10.             }
  11.  
  12.             function append(char) {
  13.                 if (empty) {
  14.                     if (char === '0') {
  15.                         return;
  16.                     }
  17.                     empty = false;
  18.                     result = char;
  19.                     update();
  20.                     return;
  21.                 }
  22.                 if (!'+-*/'.includes(result.slice(-2)[0])) {
  23.                     if ('+-*/'.includes(char)) {
  24.                         char = ' ' + char + ' ';
  25.                     }
  26.                 } else {
  27.                     if ('*/'.includes(char)) {
  28.                         return;
  29.                     }
  30.                 }
  31.                 result += char;
  32.                 update();
  33.             }
  34.  
  35.             function calculate() {
  36.                 result = eval(result).toString();
  37.                 update();
  38.             }
  39.  
  40.             function backspace() {
  41.                 if (result.length === 1) {
  42.                     empty = true;
  43.                     result = '0';
  44.                 } else {
  45.                     if (result.slice(-1) == ' ') {
  46.                         result = result.slice(0, -3);
  47.                     } else {
  48.                         result = result.slice(0, -1);
  49.                     }
  50.                 }
  51.                 update();
  52.             }
  53.         </script>
  54.         <style>
  55.             * {
  56.                 font-family: monospace;
  57.             }
  58.  
  59.             body {
  60.                 background: linear-gradient(to top, #34343E, #57575F);
  61.                 background-size: 100% 250%;
  62.                 text-align: center;
  63.                 color: #7CBDBA;
  64.             }
  65.  
  66.             #calculation-result {
  67.                 margin-right: 6px;
  68.                 font-size: 150%;
  69.             }
  70.  
  71.             .number-button {
  72.                 border: 1px solid  #7CFDFF;
  73.                 background-clip: padding-box;
  74.                 border-radius: 0.125em;
  75.                 display: inline-block;
  76.                 background: #34343E;
  77.                 text-decoration: none;
  78.                 color: #7CBDBA;
  79.                 padding: 10px;
  80.                 margin: 0.5%;
  81.             }
  82.         </style>
  83.         <title>Calculator</title>
  84.         <meta charset="utf-8">
  85.     </head>
  86.     <body>
  87.         <div id="calculator">
  88.             <a id="calculation-result">0</div>
  89.             <br>
  90.             <a class="number-button" href="#" onclick="append('1')">1</a>
  91.             <a class="number-button" href="#" onclick="append('2')">2</a>
  92.             <a class="number-button" href="#" onclick="append('3')">3</a>
  93.             <a class="number-button" href="#" onclick="append('+')">+</a>
  94.             <br>
  95.             <a class="number-button" href="#" onclick="append('4')">4</a>
  96.             <a class="number-button" href="#" onclick="append('5')">5</a>
  97.             <a class="number-button" href="#" onclick="append('6')">6</a>
  98.             <a class="number-button" href="#" onclick="append('-')">-</a>
  99.             <br>
  100.             <a class="number-button" href="#" onclick="append('7')">7</a>
  101.             <a class="number-button" href="#" onclick="append('8')">8</a>
  102.             <a class="number-button" href="#" onclick="append('9')">9</a>
  103.             <a class="number-button" href="#" onclick="append('*')">*</a>
  104.             <br>
  105.             <a class="number-button" href="#" onclick="append('.')">.</a>
  106.             <a class="number-button" href="#" onclick="append('0')">0</a>
  107.             <a class="number-button" href="#" onclick="append('/')">/</a>
  108.             <a class="number-button" href="#" onclick="calculate()">=</a>
  109.             <br>
  110.             <a class="number-button" href="#" onclick="backspace()"></a>
  111.             <br>
  112.         </div>
  113.     </body>
  114. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement