Advertisement
Guest User

Untitled

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